Skip to content

Commit

Permalink
Fix/convert inches to centimeters (#529)
Browse files Browse the repository at this point in the history
* Changes tests in order to be useful

* Fix convertInchesToCentimeters

* Adds convertCentimetersToInches issue
  • Loading branch information
lmauromb authored and BlakeGuilloud committed Oct 27, 2017
1 parent f8568b7 commit 0fb9c4c
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 11 deletions.
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");
});
});

0 comments on commit 0fb9c4c

Please sign in to comment.