diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index b22590bc6..7a8a59e8b 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -6,9 +6,21 @@ // 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) || list.length === 0) { + return null; + } + + const numbers = list.filter((item) => typeof item === "number"); + if (numbers.length === 0) { + return null; + } + numbers.sort((a, b) => a - b); + const middleIndex = Math.floor(numbers.length / 2); + + if (numbers.length % 2 === 0) { + return (numbers[middleIndex - 1] + numbers[middleIndex]) / 2; + } + return numbers[middleIndex]; +} module.exports = calculateMedian; diff --git a/Sprint-1/fix/median.test.js b/Sprint-1/fix/median.test.js index 21da654d7..604c2f351 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,17 +25,26 @@ 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]", () => { + it("doesn't modify the input array [3, 1, 2]", () => { const list = [3, 1, 2]; calculateMedian(list); 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 +55,6 @@ 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..87f98a8ea 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1 +1,14 @@ -function dedupe() {} +function dedupe(arr) { + const elements = []; + for (const item of arr) { + if (!elements.includes(item)) { + elements.push(item); + } + } + if (elements.length === arr.length) { + return arr.slice(); + } + return elements; +} + +module.exports = dedupe; diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index d7c8e3d8e..6961e3d88 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -16,13 +16,28 @@ 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"); +//test.todo("given an empty array, it returns an empty array"); +test("given an empty array, it returns an empty array", function() { + console.log(dedupe) + expect(dedupe([])).toEqual([]); + +}); // Given an array with no duplicates // When passed to the dedupe function // Then it should return a copy of the original array +test("given non duplicate array, it returns a copy of the array", function () { + const array = [1, 2, 3]; + const result = dedupe(array); + expect(result).toEqual(array); +}); // Given an array of strings or numbers -// When passed to the dedupe function +// 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. +test("given an array of strings or numbers, it returns the original occurrence of the array", function () { + const array = [1, "c", 2, 3, "d"]; + const result = dedupe(array); + expect(result).toEqual(array); +}); \ No newline at end of file diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 6dd76378e..44c14d408 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,4 +1,12 @@ function findMax(elements) { + let largestNum; + for (let i = 0; i < elements.length; i++) { + if (typeof elements[i] === "number") { + if (largestNum === undefined || elements[i] > largestNum) { + largestNum = elements[i]; + } + } + } + return largestNum; } - module.exports = findMax; diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index 82f18fd88..50c50c8a8 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -16,28 +16,48 @@ 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", function() { + expect(findMax([])).toBeUndefined(); +}); // 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", function () { + expect(findMax([5])).toBe(5); +}); // 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", function () { + expect(findMax([-1, -5, 1, 4])).toBe(4); +}); // 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 negative numbers, returns the closest to zero", function () { + expect(findMax([-3, -2, -1])).toBe(-1); +}); // Given an array with decimal numbers // When passed to the max function // Then it should return the largest decimal number +test("given an empty array, returns -Infinity", function () { + expect(findMax([6.5, 1.7, 2.3])).toBe(6.5); +}); // 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 numeric value, returns the max ignoring the non numeric value", function () { + expect(findMax([-1, -5, 1, 4])).toBe(4); +}); // 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 the least surprising value", function () { + expect(findMax(["a", "b", "c"])).toBe(undefined); +}); \ No newline at end of file diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index 9062aafe3..accc3696b 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,4 +1,20 @@ function sum(elements) { + let sum = 0; + let hasNumber = false; + + for (let i = 0; i < elements.length; i++) { + if (typeof elements[i] === "number") { + sum += elements[i]; + hasNumber = true; + } + } + if (hasNumber === false && elements.length >0) { + return undefined; + } + return sum; } +let numbers = ["c", "b", "hi", 1]; +console.log(sum(numbers)); + module.exports = sum; diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index dd0a090ca..4f03d9936 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -13,24 +13,42 @@ 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", function() { + expect(sum([])).toBe(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", function() { + expect(sum([1])).toBe(1); +}); // Given an array containing negative numbers // When passed to the sum function // Then it should still return the correct total sum +test("given a negative array, returns the sum", function() { + expect(sum([-4, -2, -1])).toBe(-7); +}); // Given an array with decimal/float numbers // When passed to the sum function // Then it should return the correct total sum +test("given a decimal array, returns the sum", function() { + expect(sum([1.9, 3.1, 2.7])).toBe(7.7); +}); // 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 a mixed array, returns only the sum of numbers ", function() { + expect(sum(["c", 3, 4, "hi", 7])).toBe(14); +}); // 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 of non-numeric value, returns undefined", function() { + expect(sum(["b", "hello", "y"])).toBe(undefined); +}); \ No newline at end of file diff --git a/Sprint-1/refactor/includes.js b/Sprint-1/refactor/includes.js index 29dad81f0..8c9ae2e66 100644 --- a/Sprint-1/refactor/includes.js +++ b/Sprint-1/refactor/includes.js @@ -1,8 +1,7 @@ // 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]; + for (const element of list) { if (element === target) { return true; } diff --git a/Sprint-1/refactor/includes.test.js b/Sprint-1/refactor/includes.test.js index 812158470..9b2b0bf92 100644 --- a/Sprint-1/refactor/includes.test.js +++ b/Sprint-1/refactor/includes.test.js @@ -36,3 +36,4 @@ test("searches for null", () => { expect(currentOutput).toEqual(targetOutput); }); +// test checked and passed. \ No newline at end of file