-
Notifications
You must be signed in to change notification settings - Fork 197
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix/convert inches to centimeters (#529)
* Changes tests in order to be useful * Fix convertInchesToCentimeters * Adds convertCentimetersToInches issue
- Loading branch information
1 parent
f8568b7
commit 0fb9c4c
Showing
4 changed files
with
50 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/** | ||
* Convert centimeters to inches where 1 cm equals 0.393701 inches | ||
* @param {number} cm | ||
* @return returned value in inches | ||
*/ | ||
function convertCentimetersToInches(cm){ | ||
// Your code goes here | ||
throw("Invalid Type"); | ||
} | ||
|
||
module.exports = convertCentimetersToInches; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,14 @@ | ||
/** | ||
* Convert inches to centimeters | ||
* Convert inches to centimeters where 1 inch equals 2.54 cm | ||
* @param {number} inches | ||
* @return returned value in centimeters | ||
*/ | ||
function convertInchesToCentimeters(inches){ | ||
//Code goes here | ||
throw new Error("Invalid Argument"); | ||
if (typeof inches === "number" && inches === inches === isFinite(inches)) { | ||
return inches * 2.54; | ||
} else { | ||
throw("Invalid Type"); | ||
} | ||
} | ||
|
||
module.exports = convertInchesToCentimeters; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
const { convertCentimetersToInches } = require("../lib"); | ||
|
||
describe("convertCentimetersToInches", () => { | ||
test("12 cm is 4.72 inches", () => { | ||
expect(convertCentimetersToInches(12)).toBe(4.72); | ||
}); | ||
|
||
test("30 cm is 11.81 inches", () => { | ||
expect(convertCentimetersToInches(30)).toBe(11.81); | ||
}); | ||
|
||
test("Throw error on invalid type", () => { | ||
expect(() => convertCentimetersToInches("a cm")).toThrow("Invalid Type"); | ||
expect(() => convertCentimetersToInches(null)).toThrow("Invalid Type"); | ||
expect(() => convertCentimetersToInches(undefined)).toThrow("Invalid Type"); | ||
expect(() => convertCentimetersToInches({})).toThrow("Invalid Type"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,22 @@ | ||
const { convertInchesToCentimeters } = require("../lib"); | ||
|
||
describe("convertInchesToCentimeters", () => { | ||
it("Should return converted inches to centimeters", () => { | ||
expect(() => convertInchesToCentimeters(5).toBe(12.7000254000508)); | ||
expect(() => convertInchesToCentimeters(7).toBe(17.78003556007112)); | ||
// add more cases | ||
test("5 inches is 12.70 cm", () => { | ||
expect(convertInchesToCentimeters(5)).toBe(12.7); | ||
}); | ||
|
||
it("Should check if parameter is valid", () => { | ||
expect(() => convertInchesToCentimeters({})).toThrow("Invalid Argument"); | ||
expect(() => convertInchesToCentimeters([1,2])).toThrow("Invalid Argument"); | ||
// add more cases | ||
test("7 inches is 17.78 cm", () => { | ||
expect(convertInchesToCentimeters(7)).toBe(17.78); | ||
}); | ||
|
||
test("12 inches is 30.48 cm", () => { | ||
expect(convertInchesToCentimeters(12)).toBe(30.48); | ||
}); | ||
|
||
test("Throw error on invalid type", () => { | ||
expect(() => convertInchesToCentimeters("a inch")).toThrow("Invalid Type"); | ||
expect(() => convertInchesToCentimeters(null)).toThrow("Invalid Type"); | ||
expect(() => convertInchesToCentimeters(undefined)).toThrow("Invalid Type"); | ||
expect(() => convertInchesToCentimeters({})).toThrow("Invalid Type"); | ||
}); | ||
}); |