diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index b22590bc6..98c165cce 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -6,9 +6,13 @@ // 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/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 781e8718a..b4b3d6142 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; \ No newline at end of file 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..77a27adb0 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,4 +1,7 @@ -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..abc39b2f8 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -12,32 +12,72 @@ We have set things up already so that this file can see your function from the o const findMax = require("./max.js"); +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. -test.todo("given an empty array, returns -Infinity"); + + 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"); + }); +}); \ No newline at end of file diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index 9062aafe3..5c909fc91 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,4 +1,8 @@ -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..806b8695d 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: +describe("sum", () => { // Given an empty array // When passed to the sum function // Then it should return 0 -test.todo("given an empty array, returns 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); + }); +}); \ No newline at end of file diff --git a/Sprint-1/refactor/includes.js b/Sprint-1/refactor/includes.js index 29dad81f0..b779c8586 100644 --- a/Sprint-1/refactor/includes.js +++ b/Sprint-1/refactor/includes.js @@ -1,6 +1,15 @@ // Refactor the implementation of includes to use a for...of loop function includes(list, target) { +for (item of list) {if (item === target) {return true;}} + return false; +} + +module.exports = includes; + + +/* Old code: + for (let index = 0; index < list.length; index++) { const element = list[index]; if (element === target) { @@ -8,6 +17,4 @@ function includes(list, target) { } } return false; -} - -module.exports = includes; +*/