From 9d6f5a7d26eac45f0880b4cc381c85e35306b840 Mon Sep 17 00:00:00 2001 From: Mohsen Zamani Date: Mon, 2 Feb 2026 22:59:34 +0000 Subject: [PATCH] Complete sprint-1 --- Sprint-1/fix/median.js | 14 ++- Sprint-1/fix/median.test.js | 22 ++++- Sprint-1/implement/dedupe.js | 8 +- Sprint-1/implement/dedupe.test.js | 31 +++++-- Sprint-1/implement/max.js | 3 + Sprint-1/implement/max.test.js | 94 +++++++++++++------ Sprint-1/implement/sum.js | 5 + Sprint-1/implement/sum.test.js | 103 ++++++++++++++++----- Sprint-1/refactor/includes.js | 8 +- Sprint-1/stretch/aoc-2018-day1/solution.js | 7 ++ 10 files changed, 220 insertions(+), 75 deletions(-) diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index b22590bc6..f8c9d4271 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -6,8 +6,18 @@ // 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]; + if (!Array.isArray(list)) return null; + const numericList = list + .filter((l) => typeof l === "number") + .sort((a, b) => a - b); + if (numericList.length <= 1) return null; + + const middleIndex = Math.floor(numericList.length / 2); + + if (numericList.length % 2 === 0) { + return (numericList[middleIndex] + numericList[middleIndex - 1]) / 2; + } + const median = numericList.splice(middleIndex, 1)[0]; return median; } 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..02a7d549b 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1 +1,7 @@ -function dedupe() {} +function dedupe(elements) { + return [...new Set(elements)]; +} + +module.exports = dedupe; + +console.log(dedupe([4542543, undefined, 4542543, 4542543, null, [], null])); diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index 23e0f8638..363a95dde 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -16,12 +16,29 @@ 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"); +describe("dedupe", () => { + it("returns an empty array if passed an empty array", () => + 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 + [ + { input: [1, 2, 3], expected: [1, 2, 3] }, + { + input: [4542543, 65756756, 433254], + expected: [4542543, 65756756, 433254], + }, + ].forEach(({ input, expected }) => + it("returns a copy of original array when passed array with no duplicates ", () => + expect(dedupe(input)).toEqual(expected)) + ); -// Given an array with strings or numbers -// When passed to the dedupe function -// Then it should remove the duplicate values, preserving the first occurence of each element + [ + { input: [1, 2, 3, "apple", 3, 2, {}], expected: [1, 2, 3, "apple", {}] }, + { + input: [4542543, undefined, 4542543, 4542543, null, [], null], + expected: [4542543, undefined, null, []], + }, + ].forEach(({ input, expected }) => + it("returns array wit unique values in their first occurrence index when passed an array with numbers and non-number values ", () => + expect(dedupe(input)).toEqual(expected)) + ); +}); diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 6dd76378e..e63ab0223 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,4 +1,7 @@ function findMax(elements) { + const numberElements = elements.filter((el) => typeof el === "number"); + if (numberElements.length === 0) return -Infinity; + return Math.max(...numberElements); } module.exports = findMax; diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index 82f18fd88..450f7d1eb 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("max()", () => { + it("returns -Infinity for empty array", () => + expect(findMax([])).toBe(-Infinity)); + + [ + { input: [43], expected: 43 }, + { input: [342], expected: 342 }, + { input: [65455453], expected: 65455453 }, + ].forEach(({ input, expected }) => + it("returns the only number in array with one number", () => + expect(findMax(input)).toBe(expected)) + ); + + [ + { input: [43, -4], expected: 43 }, + { input: [342, -45, -768, 23], expected: 342 }, + { input: [65455453, -54666, -4566, 6565], expected: 65455453 }, + ].forEach(({ input, expected }) => + it("returns the largest number in array containing negative numbers", () => + expect(findMax(input)).toBe(expected)) + ); + + [ + { input: [-43, -4], expected: -4 }, + { input: [-342, -45, -768, -23], expected: -23 }, + { input: [-65455453, -54666, -4566, -6565], expected: -4566 }, + ].forEach(({ input, expected }) => + it("returns closes number to zero in an array with only negative numbers", () => + expect(findMax(input)).toBe(expected)) + ); + + [ + { input: [43.32, -4.1], expected: 43.32 }, + { input: [342.54, -45.12, -768.76, 23.99], expected: 342.54 }, + { + input: [65455453.4533, -54666.222, -4566.322, 6565.43], + expected: 65455453.4533, + }, + ].forEach(({ input, expected }) => + it("returns the largest decimal number in an array with numbers", () => + expect(findMax(input)).toBe(expected)) + ); + + [ + { input: [1, 2, "3", null, undefined, 4], expected: 4 }, + { input: ["apple", 1, 2, 34, "banana", 4], expected: 34 }, + { input: [1, "2", 3, "4", 5], expected: 5 }, + { input: [1, "apple", 2, null, 3, undefined, 4], expected: 4 }, + { input: [3, "apple", 1, null, 2, undefined, 4, 54], expected: 54 }, + { input: ["banana", 5, 3, "apple", 1, 4, 2], expected: 5 }, + ].forEach(({ input, expected }) => + it("returns max of numbers in an array containing non-numeric values", () => + expect(findMax(input)).toEqual(expected)) + ); + + [ + ["not an array", "3", null, undefined], + ["apple", "banana"], + ["apple", null, undefined], + ["banana", {}], + ].forEach((input) => + it("returns -Infinity in array with only non-numeric values", () => + expect(findMax(input)).toBe(-Infinity)) + ); +}); diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index 9062aafe3..961cee806 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,4 +1,9 @@ function sum(elements) { + if (!Array.isArray(elements)) return 0; + return elements.reduce((acc, curr) => { + return typeof curr === "number" ? acc + curr : acc; + }, 0); } module.exports = sum; +console.log(sum([])); diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index dd0a090ca..f64ab97e0 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -10,27 +10,82 @@ 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()", () => { + it("returns 0 for empty array", () => expect(sum([])).toBe(0)); + + [ + { input: [4], expected: 4 }, + { input: [367], expected: 367 }, + { input: [7958463], expected: 7958463 }, + ].forEach(({ input, expected }) => { + it(`returns the sum for arrays with one number`, () => + expect(sum(input)).toBe(expected)); + }); + + [ + { input: [-9], expected: -9 }, + { input: [-367, -5], expected: -372 }, + { input: [-7958463, -100, -202, -6453], expected: -7965218 }, + ].forEach(({ input, expected }) => + it("returns the correct sum for array with only negative values", () => + expect(sum(input)).toBe(expected)) + ); + + [ + { input: [-9, 9], expected: 0 }, + { input: [-367, -5, 70, 2], expected: -300 }, + { input: [-7958463, -100, -202, -6453, 153, 45621], expected: -7919444 }, + ].forEach(({ input, expected }) => + it("returns the correct sum for array containing negative numbers", () => + expect(sum(input)).toBe(expected)) + ); + + [ + { input: [-9, 9, 0.1], expected: 0.1 }, + { input: [-367, -5, 70, 2, -4.567], expected: -304.567 }, + { + input: [-7958463, -100, -202, -6453, 153, 45621, -1.48, 8976.456], + expected: -7910469.024, + }, + ].forEach(({ input, expected }) => + it("returns the correct sum for array containing decimal/float numbers", () => + expect(sum(input)).toBe(expected)) + ); + + [ + { input: [-9, 9, 0.1, () => {}], expected: 0.1 }, + { + input: [-367, -5, "-234", 70, 2, { fruit: "apple" }, -4.567], + expected: -304.567, + }, + { + input: [ + -7958463, + -100, + "Iran", + -202, + -6453, + "UK", + 153, + [], + 45621, + "Egypt", + -1.48, + 8976.456, + {}, + ], + expected: -7910469.024, + }, + ].forEach(({ input, expected }) => + it("returns the correct sum for array containing decimal/float numbers", () => + expect(sum(input)).toBe(expected)) + ); + + [ + ["not an array", null, undefined, {}, []], + [("apple", null, undefined)], + ].forEach((item) => + it("returns 0 for arrays with only non-number values", () => + expect(sum(item)).toBe(0)) + ); +}); diff --git a/Sprint-1/refactor/includes.js b/Sprint-1/refactor/includes.js index 29dad81f0..3687d87f9 100644 --- a/Sprint-1/refactor/includes.js +++ b/Sprint-1/refactor/includes.js @@ -1,13 +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]; - if (element === target) { - return true; - } - } - return false; + return list.includes(target) ? true : false; } module.exports = includes; diff --git a/Sprint-1/stretch/aoc-2018-day1/solution.js b/Sprint-1/stretch/aoc-2018-day1/solution.js index e69de29bb..a5a317dc2 100644 --- a/Sprint-1/stretch/aoc-2018-day1/solution.js +++ b/Sprint-1/stretch/aoc-2018-day1/solution.js @@ -0,0 +1,7 @@ +const fs = require("fs"); + +function calculateFrequecy() { + const text = fs.readFileSync("input.txt", "utf-8"); + const ChangesInFrequency = text.trim().split(/\r?\n/).map(Number); + return ChangesInFrequency.reduce((acc, curr) => acc + curr, 0); +}