Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/convert inches to centimeters #529

Merged
merged 3 commits into from
Oct 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions lib/convertCentimetersToInches.js
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;
9 changes: 6 additions & 3 deletions lib/convertInchesToCentimeters.js
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;
18 changes: 18 additions & 0 deletions test/convertCentimetersToInches.test.js
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");
});
});
23 changes: 15 additions & 8 deletions test/convertInchesToCentimeters.test.js
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");
});
});