From 7bc29b05de29534c962c5af566f7c90e0b5d2015 Mon Sep 17 00:00:00 2001 From: Liban Jama Date: Mon, 16 Mar 2026 13:00:28 +0000 Subject: [PATCH] completed implement --- Sprint-1/implement/dedupe.js | 13 ++++++++++++- Sprint-1/implement/dedupe.test.js | 4 +++- Sprint-1/implement/max.js | 16 +++++++++++++++- Sprint-1/implement/max.test.js | 28 +++++++++++++++++++++++++++- Sprint-1/implement/sum.js | 9 +++++++++ Sprint-1/implement/sum.test.js | 24 +++++++++++++++++++++++- 6 files changed, 89 insertions(+), 5 deletions(-) diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 781e8718a..f10b813d2 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1 +1,12 @@ -function dedupe() {} +function dedupe(elements) { + const result = []; + for (let i = 0; i < elements.length; i++) { + if (!result.includes(elements[i])) { + result.push(elements[i]); + } + } + + return result; +} + +module.exports = dedupe; diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index 23e0f8638..f655bfdc7 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -16,7 +16,9 @@ E.g. dedupe([1, 2, 1]) target output: [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("given an empty array, it returns an empty array", () => { + expect(dedupe([])).toEqual([]); +}); // Given an array with no duplicates // When passed to the dedupe function diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 6dd76378e..09c343f8a 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,4 +1,18 @@ function findMax(elements) { -} + const numbersOnly = elements.filter((e) => typeof e === "number"); + + if (numbersOnly.length === 0) { + return -Infinity; + } + + let max = numbersOnly[0]; + for (let i = 1; i < numbersOnly.length; i++) { + if (numbersOnly[i] > max) { + max = numbersOnly[i]; + } + } + + return max; +} module.exports = findMax; diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index 82f18fd88..498fd0e40 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -16,28 +16,54 @@ 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([])).toBe(-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])).toBe(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 positive and negative numbers, returns the largest number", () => { + expect(findMax([-3, 7, -2, 5])).toBe(7); +}); + // 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 only negative numbers, returns the closest to zero", () => { + expect(findMax([-10, -3, -7])).toBe(-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 clargest decimal number", () => { + expect(findMax([2.7, 6.9, 10.1])).toBe(10.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 +test("given an array with non-number values, returns the max and ignore non-numeric values", () => { + expect(findMax([3, "dogs", 1, "cat", null])).toBe(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 + +test("given an array with only non-number values, returns -Infinity", () => { + expect(findMax(["a", null, true, undefined])).toBe(-Infinity); +}); diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index 9062aafe3..65ee1f272 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,4 +1,13 @@ function sum(elements) { + let total = 0; + + for (let i = 0; i < elements.length; i++) { + if (typeof elements[i] === "number") { + total += elements[i]; + } + } + + return total; } module.exports = sum; diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index dd0a090ca..226acf8fa 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -13,24 +13,46 @@ 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([])).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 one number, returns that number", () => { + expect(sum([42])).toBe(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", () => { + expect(sum([-5, -6, -1])).toBe(-12); +}); + // 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/float numbers, returns the correct total sum", () => { + expect(sum([2.75, -0.3, 0.0002])).toBeCloseTo(2.4502); +}); + // 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, ignore the non-numerical values and return the sum of the numerical elements", () => { + expect(sum(["hello", 6, true, 14])).toBe(20); +}); + // 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(["hello", undefined, "today", true])).toBe(0); +});