diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 6dd76378e..e5462ddfa 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,4 +1,6 @@ function findMax(elements) { + const numbers = elements.filter(n => typeof n === "number"); + return Math.max(...numbers); } module.exports = findMax; diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index 82f18fd88..393b73d69 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -16,28 +16,30 @@ const findMax = require("./max.js"); // When passed to the max function // Then it should return -Infinity // Delete this test.todo and replace it with a test. -test.todo("given an empty array, returns -Infinity"); +test("given an empty array, returns -Infinity", () => { + expect(findMax([])).toEqual(-Infinity); +}); -// Given an array with one number -// When passed to the max function -// Then it should return that number +test("given an array with one number, returns that number", () => { + expect(findMax([7])).toEqual(7); +}); -// Given an array with both positive and negative numbers -// When passed to the max function -// Then it should return the largest number overall +test("given an array with positive and negative numbers, returns the largest", () => { + expect(findMax([-3, 0, 5, -1])).toEqual(5); +}); -// Given an array with just negative numbers -// When passed to the max function -// Then it should return the closest one to zero +test("given an array with just negative numbers, returns the closest to zero", () => { + expect(findMax([-10, -3, -7])).toEqual(-3); +}); -// Given an array with decimal numbers -// When passed to the max function -// Then it should return the largest decimal number +test("given an array with decimal numbers, returns the largest decimal", () => { + expect(findMax([1.1, 2.9, 2.1])).toEqual(2.9); +}); -// Given an array with non-number values -// When passed to the max function -// Then it should return the max and ignore non-numeric values +test("given an array with non-number values, ignores them and returns the max", () => { + expect(findMax(["hey", 10, "hi", 60, 10])).toEqual(60); +}); -// Given an array with only non-number values -// When passed to the max function -// Then it should return the least surprising value given how it behaves for all other inputs +test("given an array with only non-number values, returns -Infinity", () => { + expect(findMax(["a", "b"])).toEqual(-Infinity); +}); diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index 9062aafe3..6230724e3 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,4 +1,5 @@ function sum(elements) { + return elements.filter(n => typeof n === "number").reduce((acc, n) => acc + n, 0); } module.exports = sum; diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index dd0a090ca..cff277f5f 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -13,24 +13,26 @@ const sum = require("./sum.js"); // Given an empty array // When passed to the sum function // Then it should return 0 -test.todo("given an empty array, returns 0") +test("given an empty array, returns 0", () => { + expect(sum([])).toEqual(0); +}); -// Given an array with just one number -// When passed to the sum function -// Then it should return that number +test("given an array with just one number, returns that number", () => { + expect(sum([42])).toEqual(42); +}); -// Given an array containing negative numbers -// When passed to the sum function -// Then it should still return the correct total sum +test("given an array containing negative numbers, returns the correct total", () => { + expect(sum([10, -5, 3])).toEqual(8); +}); -// Given an array with decimal/float numbers -// When passed to the sum function -// Then it should return the correct total sum +test("given an array with decimal numbers, returns the correct total", () => { + expect(sum([1.5, 2.5])).toEqual(4); +}); -// Given an array containing non-number values -// When passed to the sum function -// Then it should ignore the non-numerical values and return the sum of the numerical elements +test("given an array containing non-number values, ignores them and sums the rest", () => { + expect(sum(["hey", 10, "hi", 60, 10])).toEqual(80); +}); -// Given an array with only non-number values -// When passed to the sum function -// Then it should return the least surprising value given how it behaves for all other inputs +test("given an array with only non-number values, returns 0", () => { + expect(sum(["a", "b"])).toEqual(0); +}); diff --git a/Sprint-1/refactor/includes.js b/Sprint-1/refactor/includes.js index 29dad81f0..cc167f1bb 100644 --- a/Sprint-1/refactor/includes.js +++ b/Sprint-1/refactor/includes.js @@ -1,11 +1,8 @@ // Refactor the implementation of includes to use a for...of loop function includes(list, target) { - for (let index = 0; index < list.length; index++) { - const element = list[index]; - if (element === target) { - return true; - } + for (const element of list) { + if (element === target) return true; } return false; } diff --git a/Sprint-2/implement/contains.js b/Sprint-2/implement/contains.js index cd779308a..47ccafe33 100644 --- a/Sprint-2/implement/contains.js +++ b/Sprint-2/implement/contains.js @@ -1,3 +1,6 @@ -function contains() {} +function contains(obj, prop) { + if (typeof obj !== "object" || obj === null || Array.isArray(obj)) return false; + return Object.prototype.hasOwnProperty.call(obj, prop); +} module.exports = contains; diff --git a/Sprint-2/implement/contains.test.js b/Sprint-2/implement/contains.test.js index 326bdb1f2..62755ce68 100644 --- a/Sprint-2/implement/contains.test.js +++ b/Sprint-2/implement/contains.test.js @@ -20,16 +20,18 @@ as the object doesn't contains a key of 'c' // Given an empty object // When passed to contains // Then it should return false -test.todo("contains on empty object returns false"); +test("contains on empty object returns false", () => { + expect(contains({}, "a")).toEqual(false); +}); -// Given an object with properties -// When passed to contains with an existing property name -// Then it should return true +test("returns true when object has the property", () => { + expect(contains({ a: 1, b: 2 }, "a")).toEqual(true); +}); -// Given an object with properties -// When passed to contains with a non-existent property name -// Then it should return false +test("returns false when object does not have the property", () => { + expect(contains({ a: 1, b: 2 }, "c")).toEqual(false); +}); -// Given invalid parameters like an array -// When passed to contains -// Then it should return false or throw an error +test("returns false when passed an array", () => { + expect(contains([1, 2, 3], "0")).toEqual(false); +});