diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index b22590bc6..1f42edab0 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -6,9 +6,24 @@ // or 'list' has mixed values (the function is expected to sort only numbers). function calculateMedian(list) { - const middleIndex = Math.floor(list.length / 2); - const median = list.splice(middleIndex, 1)[0]; - return median; + if (!Array.isArray(list)) { + return null; + } + let numberArray = list.filter((n) => Number.isFinite(n)); + if (numberArray.length == 0) { + return null; + } + numberArray.sort((a, b) => a - b); + + if (numberArray.length % 2 == 1) { + return numberArray[(numberArray.length - 1) / 2]; + } else { + return ( + (numberArray[numberArray.length / 2] + + numberArray[numberArray.length / 2 - 1]) / + 2 + ); + } } module.exports = calculateMedian; diff --git a/Sprint-1/fix/median.test.js b/Sprint-1/fix/median.test.js index 21da654d7..c262c3776 100644 --- a/Sprint-1/fix/median.test.js +++ b/Sprint-1/fix/median.test.js @@ -13,7 +13,8 @@ describe("calculateMedian", () => { { input: [1, 2, 3, 4], expected: 2.5 }, { input: [1, 2, 3, 4, 5, 6], expected: 3.5 }, ].forEach(({ input, expected }) => - it(`returns the median for [${input}]`, () => expect(calculateMedian(input)).toEqual(expected)) + it(`returns the median for [${input}]`, () => + expect(calculateMedian(input)).toEqual(expected)) ); [ @@ -24,7 +25,8 @@ describe("calculateMedian", () => { { input: [110, 20, 0], expected: 20 }, { input: [6, -2, 2, 12, 14], expected: 6 }, ].forEach(({ input, expected }) => - it(`returns the correct median for unsorted array [${input}]`, () => expect(calculateMedian(input)).toEqual(expected)) + it(`returns the correct median for unsorted array [${input}]`, () => + expect(calculateMedian(input)).toEqual(expected)) ); it("doesn't modify the input array [3, 1, 2]", () => { @@ -33,8 +35,17 @@ describe("calculateMedian", () => { expect(list).toEqual([3, 1, 2]); }); - [ 'not an array', 123, null, undefined, {}, [], ["apple", null, undefined] ].forEach(val => - it(`returns null for non-numeric array (${val})`, () => expect(calculateMedian(val)).toBe(null)) + [ + "not an array", + 123, + null, + undefined, + {}, + [], + ["apple", null, undefined], + ].forEach((val) => + it(`returns null for non-numeric array (${val})`, () => + expect(calculateMedian(val)).toBe(null)) ); [ @@ -45,6 +56,7 @@ describe("calculateMedian", () => { { input: [3, "apple", 1, null, 2, undefined, 4], expected: 2.5 }, { input: ["banana", 5, 3, "apple", 1, 4, 2], expected: 3 }, ].forEach(({ input, expected }) => - it(`filters out non-numeric values and calculates the median for [${input}]`, () => expect(calculateMedian(input)).toEqual(expected)) + it(`filters out non-numeric values and calculates the median for [${input}]`, () => + expect(calculateMedian(input)).toEqual(expected)) ); }); diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 781e8718a..4ead32366 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1 +1,11 @@ -function dedupe() {} +function dedupe(array) { + const unique = []; + array.forEach((item) => { + if (!unique.includes(item)) { + unique.push(item); + } + }); + return unique; +} + +module.exports = dedupe; diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index d7c8e3d8e..acc42cc42 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -16,13 +16,39 @@ E.g. dedupe([1, 2, 1]) returns [1, 2] // Given an empty array // When passed to the dedupe function // Then it should return an empty array -test.todo("given an empty array, it returns an empty array"); +describe("dedupe", () => { + [ + { input: [], expected: [] }, + ].forEach(({ input, expected }) => + it(`returns empty array for [${input}]`, () => expect(dedupe(input)).toEqual(expected)) + ); + // Given an array with no duplicates // When passed to the dedupe function // Then it should return a copy of the original array + [ + { input: [1, 2, 3], expected: [1, 2, 3] }, + { input: [3, 2, 1, 4, 5], expected: [3, 2, 1, 4, 5] }, + { input: ["hello", "a", "b", "c"], expected: ["hello", "a", "b", "c"] }, + { input: [1, 2, "hello", 4, "a", 6], expected: [1, 2, "hello", 4, "a", 6] }, + ].forEach(({ input, expected }) => + it(`returns the median for [${input}]`, () => expect(dedupe(input)).toEqual(expected)) + ); + // Given an array of strings or numbers // When passed to the dedupe function // Then it should return a new array with duplicates removed while preserving the // first occurrence of each element from the original array. + + [ + { input: [1, 1, 2, 2, 3, 3,], expected: [1, 2, 3] }, + { input: ["a", "a", "b", "b", "c", "c"], expected: ["a", "b", "c"] }, + { input: ["qwe", "qwe", 3, 3, "hello", "hello" , "hello"], expected: ["qwe", 3, "hello"] }, + { input: ["a", "a", "a", "a"], expected: ["a"] }, + { input: [1, 2, 2, 2, 2, 2], expected: [1, 2] }, + ].forEach(({ input, expected }) => + it(`returns the median for [${input}]`, () => expect(dedupe(input)).toEqual(expected)) + ); +}); \ No newline at end of file diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 6dd76378e..5c779794e 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,4 +1,10 @@ -function findMax(elements) { +function findMax(list) { + const numbers = list.filter((n) => Number.isFinite(n)); + if (numbers.length == 0) { + return "-Infinity"; + } else { + 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..6db256392 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -12,32 +12,68 @@ We have set things up already so that this file can see your function from the o const findMax = require("./max.js"); -// Given an empty array -// 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"); - -// Given an array with one number -// When passed to the max function -// Then it should return that number - -// Given an array with both positive and negative numbers -// When passed to the max function -// Then it should return the largest number overall - -// Given an array with just negative numbers -// When passed to the max function -// Then it should return the closest one to zero - -// Given an array with decimal numbers -// When passed to the max function -// Then it should return the largest decimal number - -// Given an array with non-number values -// When passed to the max function -// Then it should return the max and ignore non-numeric values - -// 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 +describe("calculateMedian", () => { + // Given an empty array + // When passed to the max function + // Then it should return -Infinity + // Delete this test.todo and replace it with a test. + + it("empty array returns -Infinity", () => { + const list = []; + expect(findMax(list)).toEqual("-Infinity"); + }); + + // Given an array with one number + // When passed to the max function + // Then it should return that number + + it("array with one number returns that number", () => { + const list = [2]; + expect(findMax(list)).toEqual(2); + }); + + // Given an array with both positive and negative numbers + // When passed to the max function + // Then it should return the largest number overall + + it("array with +ve and -ve numbers, returns largest number overall", () => { + const list = [10, -15]; + expect(findMax(list)).toEqual(10); + }); + + // Given an array with just negative numbers + // When passed to the max function + // Then it should return the closest one to zero + + it("array -ve numbers, returns closest to zero", () => { + const list = [-1, -2, -3, -15]; + expect(findMax(list)).toEqual(-1); + }); + + // Given an array with decimal numbers + // When passed to the max function + // Then it should return the largest decimal number + + it("array with decimal numbers should return largest decimal number", () => { + const list = [1.9, 2.8999, 3.1]; + expect(findMax(list)).toEqual(3.1); + }); + + // Given an array with non-number values + // When passed to the max function + // Then it should return the max and ignore non-numeric values + + it("array with non-number values, returns the max and ignore non-numeric values", () => { + const list = [1, "a", 2, "b", 3, "c"]; + expect(findMax(list)).toEqual(3); + }); + + // 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 + + it("array with only non-number values, return the least surprising value given how it behaves for all other inputs", () => { + const list = ["a", "b", "c"]; + expect(findMax(list)).toEqual("-Infinity"); + }); +}); diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index 9062aafe3..249515398 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,4 +1,10 @@ -function sum(elements) { +function sum(list) { + const numbers = list.filter((n) => Number.isFinite(n)); + let total = 0; + for (n of numbers) { + total += n; + } + return total; } module.exports = sum; diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index dd0a090ca..832e11353 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -10,27 +10,56 @@ const sum = require("./sum.js"); // Acceptance Criteria: -// Given an empty array -// When passed to the sum function -// Then it should return 0 -test.todo("given an empty array, returns 0") - -// Given an array with just one number -// When passed to the sum function -// Then it should return that number - -// Given an array containing negative numbers -// When passed to the sum function -// Then it should still return the correct total sum - -// Given an array with decimal/float numbers -// When passed to the sum function -// Then it should return the correct total sum - -// 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 - -// 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 +describe("sum", () => { + // Given an empty array + // When passed to the sum function + // Then it should return 0 + it("empty array returns 0", () => { + const list = []; + expect(sum(list)).toEqual(0); + }); + + // Given an array with just one number + // When passed to the sum function + // Then it should return that number + it("array with one number returns number", () => { + const list = [1]; + expect(sum(list)).toEqual(1); + }); + + // Given an array containing negative numbers + // When passed to the sum function + // Then it should still return the correct total sum + + it("array with negative numbers returns correct sum", () => { + const list = [-1, -2, -3]; + expect(sum(list)).toEqual(-6); + }); + + // Given an array with decimal/float numbers + // When passed to the sum function + // Then it should return the correct total sum + + it("array with decimal numbers returns correct sum", () => { + const list = [1.1, 2.2, 3.3]; + expect(sum(list)).toEqual(6.6); + }); + + // 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 + + it("array with non-number values returns sum of numerical values only", () => { + const list = [1, 2, "apple", 4, "banana", 3]; + expect(sum(list)).toEqual(10); + }); + + // 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 + + it("array with non-number values only returns least surprising output", () => { + const list = ["a", "b", "c"]; + expect(sum(list)).toEqual(0); + }); +}); diff --git a/Sprint-1/refactor/includes.js b/Sprint-1/refactor/includes.js index 29dad81f0..c5b01e86c 100644 --- a/Sprint-1/refactor/includes.js +++ b/Sprint-1/refactor/includes.js @@ -1,9 +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) { + for (item of list) { + if (item === target) { return true; } } @@ -11,3 +10,14 @@ function includes(list, target) { } module.exports = includes; + +/* Old code: + + for (let index = 0; index < list.length; index++) { + const element = list[index]; + if (element === target) { + return true; + } + } + return false; +*/