From 120dd163337ac9fdf81ce7026975fb2d80d7b9c5 Mon Sep 17 00:00:00 2001 From: mandip sanger Date: Mon, 3 Aug 2026 14:43:28 +0100 Subject: [PATCH 01/17] dedupe errors fix --- Sprint-1/implement/dedupe.test.js | 37 ++++++++++++++++++++++++++----- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index d7c8e3d8e..fcff895ca 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -1,4 +1,3 @@ -const dedupe = require("./dedupe.js"); /* Dedupe Array @@ -16,13 +15,41 @@ 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"); + +const dedupe = require("./dedupe"); + +// Given an empty array +// When passed to the dedupe function +// Then it should return an empty array +test("should return an empty array when given 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 +test("should return a copy of the original array when there are no duplicates", () => { + expect(dedupe([1, 2, 3, 4])).toEqual([1, 2, 3, 4]); +}); + +// Given an array of strings with duplicates +// When passed to the dedupe function +// Then it should remove duplicate strings while preserving the first occurrence +test("should remove duplicate strings while preserving the first occurrence", () => { + expect(dedupe(["a", "a", "a", "b", "b", "c"])).toEqual(["a", "b", "c"]); +}); + +// Given an array of numbers with duplicates +// When passed to the dedupe function +// Then it should remove duplicate numbers while preserving the first occurrence +test("should remove duplicate numbers while preserving the first occurrence", () => { + expect(dedupe([5, 1, 1, 2, 3, 2, 5, 8])).toEqual([5, 1, 2, 3, 8]); +}); -// Given an array of strings or numbers +// Given an array with one element // 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. +// Then it should return the same array +test("should return the same array when it contains only one element", () => { + expect(dedupe([42])).toEqual([42]); +}); From eb7a06ec80b20786f0003402d9c44547d5d2840d Mon Sep 17 00:00:00 2001 From: mandip sanger Date: Tue, 4 Aug 2026 20:29:18 +0100 Subject: [PATCH 02/17] median --- Sprint-1/fix/median.test.js | 117 ++++++++++++++++++++++++------------ 1 file changed, 77 insertions(+), 40 deletions(-) diff --git a/Sprint-1/fix/median.test.js b/Sprint-1/fix/median.test.js index 21da654d7..c77096371 100644 --- a/Sprint-1/fix/median.test.js +++ b/Sprint-1/fix/median.test.js @@ -6,45 +6,82 @@ const calculateMedian = require("./median.js"); +// describe("calculateMedian", () => { +// [ +// { input: [1, 2, 3], expected: 2 }, +// { input: [1, 2, 3, 4, 5], expected: 3 }, +// { 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)) +// ); + +// [ +// { input: [3, 1, 2], expected: 2 }, +// { input: [5, 1, 3, 4, 2], expected: 3 }, +// { input: [4, 2, 1, 3], expected: 2.5 }, +// { input: [6, 1, 5, 3, 2, 4], expected: 3.5 }, +// { 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("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)) +// ); + +// [ +// { input: [1, 2, "3", null, undefined, 4], expected: 2 }, +// { input: ["apple", 1, 2, 3, "banana", 4], expected: 2.5 }, +// { input: [1, "2", 3, "4", 5], expected: 3 }, +// { input: [1, "apple", 2, null, 3, undefined, 4], expected: 2.5 }, +// { 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)) +// ); +// }); + describe("calculateMedian", () => { - [ - { input: [1, 2, 3], expected: 2 }, - { input: [1, 2, 3, 4, 5], expected: 3 }, - { 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)) - ); - - [ - { input: [3, 1, 2], expected: 2 }, - { input: [5, 1, 3, 4, 2], expected: 3 }, - { input: [4, 2, 1, 3], expected: 2.5 }, - { input: [6, 1, 5, 3, 2, 4], expected: 3.5 }, - { 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("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)) - ); - - [ - { input: [1, 2, "3", null, undefined, 4], expected: 2 }, - { input: ["apple", 1, 2, 3, "banana", 4], expected: 2.5 }, - { input: [1, "2", 3, "4", 5], expected: 3 }, - { input: [1, "apple", 2, null, 3, undefined, 4], expected: 2.5 }, - { 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)) - ); + test("returns null for an empty array", () => { + expect(calculateMedian([])).toBeNull(); + }); + + test("returns null when there are no numbers", () => { + expect(calculateMedian(["a", "b", null])).toBeNull(); + }); + + test("returns the median for an odd number of elements", () => { + expect(calculateMedian([1, 2, 3])).toEqual(2); + expect(calculateMedian([5, 1, 3])).toEqual(3); + }); + + test("returns the median for an even number of elements", () => { + expect(calculateMedian([1, 2, 3, 4])).toEqual(2.5); + expect(calculateMedian([6, 2, 4, 8])).toEqual(5); + }); + + test("ignores non-number values", () => { + expect(calculateMedian([1, "a", 2, 3])).toEqual(2); + expect(calculateMedian([5, "x", 1, 3])).toEqual(3); + }); + + test("ignores NaN values", () => { + expect(calculateMedian([1, NaN, 2, 3])).toEqual(2); + }); + + test("returns the single number when the array has one number", () => { + expect(calculateMedian([7])).toEqual(7); + }); + + test("returns the single number when mixed with non-numbers", () => { + expect(calculateMedian(["a", 7, null])).toEqual(7); + }); }); From 1792362a496e8537a670aa0da58d322118fb4da4 Mon Sep 17 00:00:00 2001 From: mandip sanger Date: Tue, 4 Aug 2026 20:29:33 +0100 Subject: [PATCH 03/17] dedupe --- Sprint-1/implement/dedupe.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 781e8718a..b653a8e91 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1 +1,8 @@ -function dedupe() {} +function dedupe(arr) { + return [...new Set(arr)]; +} + +module.exports = dedupe; + +console.log(dedupe(["a", "a", "a", "b", "b", "c"])); // ['a', 'b', 'c'] +console.log(dedupe([5, 1, 1, 2, 3, 2, 5, 8])); // [5, 1, 2, 3, 8] From b9f23922cb5f30bd6aa1acfdc1f825318b03419b Mon Sep 17 00:00:00 2001 From: mandip sanger Date: Tue, 4 Aug 2026 20:30:02 +0100 Subject: [PATCH 04/17] fix errors --- Sprint-1/fix/median.js | 31 ++++++++++++++++++++--- Sprint-1/implement/max.js | 3 +++ Sprint-1/implement/max.test.js | 1 + Sprint-1/implement/sum.js | 9 +++++++ Sprint-1/implement/sum.test.js | 45 ++++++++++++++++++++++++++++++++-- 5 files changed, 84 insertions(+), 5 deletions(-) diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index b22590bc6..c58e757a7 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -4,11 +4,36 @@ // Hint: Please consider scenarios when 'list' doesn't have numbers (the function is expected to return null) // or 'list' has mixed values (the function is expected to sort only numbers). - function calculateMedian(list) { + list = list.filter((item) => typeof item === "number" && !isNaN(item)); + + if (list.length === 0) { + return null; + } + + list.sort((a, b) => a - b); + const middleIndex = Math.floor(list.length / 2); - const median = list.splice(middleIndex, 1)[0]; - return median; + + if (list.length % 2 === 1) { + return list[middleIndex]; + } + + return (list[middleIndex - 1] + list[middleIndex]) / 2; } module.exports = calculateMedian; + +// function calculateMedian(list) { +// if (list.length === 0) return null; +// list = list.filter((item) => typeof item === "number" && !isNaN(item)); +// list.sort((a, b) => a - b); +// const middleIndex = Math.floor(list.length / 2); +// if (list.length % 2 === 1) { +// return list[middleIndex]; //odd number if elements +// } + +// return (list[middleIndex - 1] + list[middleIndex]) / 2; +// } + +// module.exports = calculateMedian; diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 6dd76378e..6a712f0c1 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,4 +1,7 @@ function findMax(elements) { + console.log(elements); + console.log("Hello from Abdi"); + console.log("Hello from Mandy"); } module.exports = findMax; diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index 82f18fd88..e9fbf861e 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -11,6 +11,7 @@ We have set things up already so that this file can see your function from the o */ const findMax = require("./max.js"); +findMax(); // Given an empty array // When passed to the max function diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index 9062aafe3..d22a19af5 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" && !isNaN(elements[i])) { + 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..bbdcb29ce 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -9,28 +9,69 @@ E.g. sum(['hey', 10, 'hi', 60, 10]), target output: 80 (ignore any non-numerical 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") + +test("returns 0 for empty array", () => { + const list = []; + const currentOutput = sum(list); + const targetOutput = 0; + + expect(currentOutput).toEqual(targetOutput); +}); // Given an array with just one number // When passed to the sum function // Then it should return that number +test("sum of 1 number", () => { + const list = [1]; + const curretOutput = sum(list); + + const targetOutput = 1; + expect(currenOutput).toEqual(targetOutput); +}); // Given an array containing negative numbers // When passed to the sum function // Then it should still return the correct total sum +test("sum of negative number in arrays", () => { + const list = [-5]; + const curretOutput = sum(list); + const targetOutput = -5; + expect(currenOutput).toEqual(targetOutput); +}); + // Given an array with decimal/float numbers // When passed to the sum function // Then it should return the correct total sum +test("sum of decimal/float number in arrays", () => { + const list = [4.5, 2.5]; + const curretOutput = sum(list); + const targetOutput = 7; + expect(currenOutput).toEqual(targetOutput); +}); + // 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("ignores non-number values", () => { + const list = ["hey", 10, "hi", 60, 10]; + const currentOutput = sum(list); + const targetOutput = 80; + + expect(currentOutput).toEqual(targetOutput); +}); + +test("ignores NaN values when calculating the sum", () => { + const list = [4, NaN, 6]; + const currentOutput = sum(list); + const targetOutput = 10; + expect(currentOutput).toEqual(targetOutput); +}); // 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 From 3ecea9e614ae40c211ac8321dc02d18d355bd697 Mon Sep 17 00:00:00 2001 From: mandip sanger Date: Tue, 4 Aug 2026 20:59:10 +0100 Subject: [PATCH 05/17] max test --- Sprint-1/implement/max.test.js | 41 ++++++++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index e9fbf861e..cd23d7558 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -11,34 +11,71 @@ We have set things up already so that this file can see your function from the o */ const findMax = require("./max.js"); -findMax(); // 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"); + +test("returns -Infinity for an empty array", () => { + const currentOutput = max([]); + const targetOutput = -Infinity; + + expect(currentOutput).toEqual(targetOutput); +}); // Given an array with one number // When passed to the max function // Then it should return that number +test("returns with one number", () => { + const currentOutput = max([3]); + const targetOutput = 3; + expect(currentOutput).toEqual(targetOutput); +}); + // Given an array with both positive and negative numbers // When passed to the max function // Then it should return the largest number overall +test("returns largest number", () => { + const currentOutput = max([3, -10]); + const targetOutput = 3; + expect(currentOutput).toEqual(targetOutput); +}); // Given an array with just negative numbers // When passed to the max function // Then it should return the closest one to zero +test("return closest to 0", () => { + const currentOutput = max([-7, -3, -10]); + const targetOutput = -3; + expect(currentOutput).toEqual(targetOutput); +}); // Given an array with decimal numbers // When passed to the max function // Then it should return the largest decimal number +test("return to largest decimal number", () => { + const currentOutput = max([2.5, 5.6]); + const targetOutput = 5.6; + expect(currentOutput).toEqual(targetOutput); +}); // 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("return to max and ignore non-numeric values", () => { + const currentOutput = max([7, 4, -10], "b", "f"); + const targetOutput = -7; + expect(currentOutput).toEqual(targetOutput); +}); + // 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("return the least surprising value", () => { + const currentOutput = max([NaN, "hi", true]); + const targetOutput = null; + expect(currentOutput).toEqual(targetOutput); +}); From b27a4d8731fbc6e1529b7c6c7098e4401520e7e8 Mon Sep 17 00:00:00 2001 From: mandip sanger Date: Thu, 6 Aug 2026 10:22:27 +0100 Subject: [PATCH 06/17] fix failing tests --- Sprint-1/implement/sum.test.js | 13 ++++---- Sprint-2/implement/querystring.js | 34 +++++++++++++++++-- Sprint-2/implement/querystring.test.js | 2 +- Sprint-2/implement/tally.test.js | 46 +++++++++++++++++++++++++- 4 files changed, 84 insertions(+), 11 deletions(-) diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index bbdcb29ce..7287551f0 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -7,7 +7,6 @@ E.g. sum(['hey', 10, 'hi', 60, 10]), target output: 80 (ignore any non-numerical */ const sum = require("./sum.js"); - // Acceptance Criteria: // Given an empty array // When passed to the sum function @@ -27,10 +26,10 @@ test("returns 0 for empty array", () => { test("sum of 1 number", () => { const list = [1]; - const curretOutput = sum(list); + const currentOutput = sum(list); const targetOutput = 1; - expect(currenOutput).toEqual(targetOutput); + expect(currentOutput).toEqual(targetOutput); }); // Given an array containing negative numbers // When passed to the sum function @@ -38,9 +37,9 @@ test("sum of 1 number", () => { test("sum of negative number in arrays", () => { const list = [-5]; - const curretOutput = sum(list); + const currentOutput = sum(list); const targetOutput = -5; - expect(currenOutput).toEqual(targetOutput); + expect(currentOutput).toEqual(targetOutput); }); // Given an array with decimal/float numbers @@ -49,9 +48,9 @@ test("sum of negative number in arrays", () => { test("sum of decimal/float number in arrays", () => { const list = [4.5, 2.5]; - const curretOutput = sum(list); + const currentOutput = sum(list); const targetOutput = 7; - expect(currenOutput).toEqual(targetOutput); + expect(currentOutput).toEqual(targetOutput); }); // Given an array containing non-number values diff --git a/Sprint-2/implement/querystring.js b/Sprint-2/implement/querystring.js index 45ec4e5f3..c5ccb6ff8 100644 --- a/Sprint-2/implement/querystring.js +++ b/Sprint-2/implement/querystring.js @@ -1,13 +1,43 @@ function parseQueryString(queryString) { const queryParams = {}; + if (queryString.length === 0) { return queryParams; } + const keyValuePairs = queryString.split("&"); for (const pair of keyValuePairs) { - const [key, value] = pair.split("="); - queryParams[key] = value; + // Ignore empty pairs + if (pair === "") continue; + + const equalIndex = pair.indexOf("="); + + let key; + let value; + + if (equalIndex === -1) { + key = pair; + value = ""; + } else { + key = pair.slice(0, equalIndex); + value = pair.slice(equalIndex + 1); + } + + // Replace + with spaces and decode URL encoding + key = decodeURIComponent(key.replace(/\+/g, " ")); + value = decodeURIComponent(value.replace(/\+/g, " ")); + + // Handle duplicate keys + if (queryParams[key] !== undefined) { + if (Array.isArray(queryParams[key])) { + queryParams[key].push(value); + } else { + queryParams[key] = [queryParams[key], value]; + } + } else { + queryParams[key] = value; + } } return queryParams; diff --git a/Sprint-2/implement/querystring.test.js b/Sprint-2/implement/querystring.test.js index 328b8df61..e9e0e9abd 100644 --- a/Sprint-2/implement/querystring.test.js +++ b/Sprint-2/implement/querystring.test.js @@ -3,7 +3,7 @@ // Below are some test cases the implementation doesn't handle well. // Fix the implementation for these tests, and try to think of as many other edge cases as possible - write tests and fix those too. -const parseQueryString = require("./querystring.js") +const parseQueryString = require("./querystring.js"); test("should parse values containing '='", () => { expect(parseQueryString("equation=a=b-2")).toEqual({ diff --git a/Sprint-2/implement/tally.test.js b/Sprint-2/implement/tally.test.js index 2ceffa8dd..7a06b98f5 100644 --- a/Sprint-2/implement/tally.test.js +++ b/Sprint-2/implement/tally.test.js @@ -23,7 +23,6 @@ const tally = require("./tally.js"); // Given an empty array // When passed to tally // Then it should return an empty object -test.todo("tally on an empty array returns an empty object"); // Given an array with duplicate items // When passed to tally @@ -32,3 +31,48 @@ test.todo("tally on an empty array returns an empty object"); // Given an invalid input like a string // When passed to tally // Then it should throw an error + +// Given an empty array +// When passed to tally +// Then it should return an empty object +test("tally on an empty array returns an empty object", () => { + expect(tally([])).toEqual({}); +}); + +// Given an array with one item +// When passed to tally +// Then it should return the item count +test("returns count of one item", () => { + expect(tally(["a"])).toEqual({ + a: 1, + }); +}); + +// Given an array with duplicate items +// When passed to tally +// Then it should return counts for each unique item +test("returns counts for duplicate items", () => { + expect(tally(["a", "a", "a", "b", "c"])).toEqual({ + a: 3, + b: 1, + c: 1, + }); +}); + +// Given an array with different item types +// When passed to tally +// Then it should count each unique item +test("counts numbers correctly", () => { + expect(tally([1, 2, 2, 3, 3, 3])).toEqual({ + 1: 1, + 2: 2, + 3: 3, + }); +}); + +// Given an invalid input like a string +// When passed to tally +// Then it should throw an error +test("throws an error when input is not an array", () => { + expect(() => tally("hello")).toThrow(); +}); From 01d183154150d8662003a8dd719b023432f694d5 Mon Sep 17 00:00:00 2001 From: mandip sanger Date: Thu, 6 Aug 2026 20:44:24 +0100 Subject: [PATCH 07/17] implemented max --- Sprint-1/implement/max.js | 12 +++++++--- Sprint-1/implement/max.test.js | 44 +++++++++++++++++----------------- 2 files changed, 31 insertions(+), 25 deletions(-) diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 6a712f0c1..9de46d2b6 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,7 +1,13 @@ function findMax(elements) { - console.log(elements); - console.log("Hello from Abdi"); - console.log("Hello from Mandy"); + const numbersOnly = elements.filter( + (item) => typeof item === "number" && !Number.isNaN(item) + ); + if (numbersOnly.length === 0) { + return -Infinity; + } + const sorted = [...numbersOnly].sort((a, b) => a - b); + const max = sorted[sorted.length - 1]; + return max; } module.exports = findMax; diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index cd23d7558..1176fb547 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -1,11 +1,11 @@ -/* Find the maximum element of an array of numbers +/* Find the findMaximum element of an array of numbers In this kata, you will need to implement a function that find the largest numerical element of an array. -E.g. max([30, 50, 10, 40]), target output: 50 -E.g. max(['hey', 10, 'hi', 60, 10]), target output: 60 (sum ignores any non-numerical elements) +E.g. findMax([30, 50, 10, 40]), target output: 50 +E.g. findMax(['hey', 10, 'hi', 60, 10]), target output: 60 (sum ignores any non-numerical elements) -You should implement this function in max.js, and add tests for it in this file. +You should implement this function in findMax.js, and add tests for it in this file. We have set things up already so that this file can see your function from the other file. */ @@ -13,69 +13,69 @@ 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 +// When passed to the findMax function // Then it should return -Infinity // Delete this test.todo and replace it with a test. test("returns -Infinity for an empty array", () => { - const currentOutput = max([]); + const currentOutput = findMax([]); const targetOutput = -Infinity; expect(currentOutput).toEqual(targetOutput); }); // Given an array with one number -// When passed to the max function +// When passed to the findMax function // Then it should return that number test("returns with one number", () => { - const currentOutput = max([3]); + const currentOutput = findMax([3]); const targetOutput = 3; expect(currentOutput).toEqual(targetOutput); }); // Given an array with both positive and negative numbers -// When passed to the max function +// When passed to the findMax function // Then it should return the largest number overall test("returns largest number", () => { - const currentOutput = max([3, -10]); + const currentOutput = findMax([3, -10]); const targetOutput = 3; expect(currentOutput).toEqual(targetOutput); }); // Given an array with just negative numbers -// When passed to the max function +// When passed to the findMax function // Then it should return the closest one to zero test("return closest to 0", () => { - const currentOutput = max([-7, -3, -10]); + const currentOutput = findMax([-7, -3, -10]); const targetOutput = -3; expect(currentOutput).toEqual(targetOutput); }); // Given an array with decimal numbers -// When passed to the max function +// When passed to the findMax function // Then it should return the largest decimal number test("return to largest decimal number", () => { - const currentOutput = max([2.5, 5.6]); + const currentOutput = findMax([2.5, 5.6]); const targetOutput = 5.6; expect(currentOutput).toEqual(targetOutput); }); // Given an array with non-number values -// When passed to the max function -// Then it should return the max and ignore non-numeric values +// When passed to the findMax function +// Then it should return the findMax and ignore non-numeric values -test("return to max and ignore non-numeric values", () => { - const currentOutput = max([7, 4, -10], "b", "f"); - const targetOutput = -7; +test("return to findMax and ignore non-numeric values", () => { + const currentOutput = findMax([7, 4, -10, "b", "f"]); + const targetOutput = 7; expect(currentOutput).toEqual(targetOutput); }); // Given an array with only non-number values -// When passed to the max function +// When passed to the findMax function // Then it should return the least surprising value given how it behaves for all other inputs test("return the least surprising value", () => { - const currentOutput = max([NaN, "hi", true]); - const targetOutput = null; + const currentOutput = findMax([NaN, "hi", true]); + const targetOutput = -Infinity; expect(currentOutput).toEqual(targetOutput); }); From 49dd8e3dd72ea51c952abe3d5ce9d5d24750262c Mon Sep 17 00:00:00 2001 From: mandip sanger Date: Thu, 6 Aug 2026 21:01:00 +0100 Subject: [PATCH 08/17] replace original --- Sprint-2/implement/querystring.js | 34 ++----------------- Sprint-2/implement/querystring.test.js | 2 +- Sprint-2/implement/tally.test.js | 46 +------------------------- 3 files changed, 4 insertions(+), 78 deletions(-) diff --git a/Sprint-2/implement/querystring.js b/Sprint-2/implement/querystring.js index c5ccb6ff8..45ec4e5f3 100644 --- a/Sprint-2/implement/querystring.js +++ b/Sprint-2/implement/querystring.js @@ -1,43 +1,13 @@ function parseQueryString(queryString) { const queryParams = {}; - if (queryString.length === 0) { return queryParams; } - const keyValuePairs = queryString.split("&"); for (const pair of keyValuePairs) { - // Ignore empty pairs - if (pair === "") continue; - - const equalIndex = pair.indexOf("="); - - let key; - let value; - - if (equalIndex === -1) { - key = pair; - value = ""; - } else { - key = pair.slice(0, equalIndex); - value = pair.slice(equalIndex + 1); - } - - // Replace + with spaces and decode URL encoding - key = decodeURIComponent(key.replace(/\+/g, " ")); - value = decodeURIComponent(value.replace(/\+/g, " ")); - - // Handle duplicate keys - if (queryParams[key] !== undefined) { - if (Array.isArray(queryParams[key])) { - queryParams[key].push(value); - } else { - queryParams[key] = [queryParams[key], value]; - } - } else { - queryParams[key] = value; - } + const [key, value] = pair.split("="); + queryParams[key] = value; } return queryParams; diff --git a/Sprint-2/implement/querystring.test.js b/Sprint-2/implement/querystring.test.js index e9e0e9abd..328b8df61 100644 --- a/Sprint-2/implement/querystring.test.js +++ b/Sprint-2/implement/querystring.test.js @@ -3,7 +3,7 @@ // Below are some test cases the implementation doesn't handle well. // Fix the implementation for these tests, and try to think of as many other edge cases as possible - write tests and fix those too. -const parseQueryString = require("./querystring.js"); +const parseQueryString = require("./querystring.js") test("should parse values containing '='", () => { expect(parseQueryString("equation=a=b-2")).toEqual({ diff --git a/Sprint-2/implement/tally.test.js b/Sprint-2/implement/tally.test.js index 7a06b98f5..2ceffa8dd 100644 --- a/Sprint-2/implement/tally.test.js +++ b/Sprint-2/implement/tally.test.js @@ -23,6 +23,7 @@ const tally = require("./tally.js"); // Given an empty array // When passed to tally // Then it should return an empty object +test.todo("tally on an empty array returns an empty object"); // Given an array with duplicate items // When passed to tally @@ -31,48 +32,3 @@ const tally = require("./tally.js"); // Given an invalid input like a string // When passed to tally // Then it should throw an error - -// Given an empty array -// When passed to tally -// Then it should return an empty object -test("tally on an empty array returns an empty object", () => { - expect(tally([])).toEqual({}); -}); - -// Given an array with one item -// When passed to tally -// Then it should return the item count -test("returns count of one item", () => { - expect(tally(["a"])).toEqual({ - a: 1, - }); -}); - -// Given an array with duplicate items -// When passed to tally -// Then it should return counts for each unique item -test("returns counts for duplicate items", () => { - expect(tally(["a", "a", "a", "b", "c"])).toEqual({ - a: 3, - b: 1, - c: 1, - }); -}); - -// Given an array with different item types -// When passed to tally -// Then it should count each unique item -test("counts numbers correctly", () => { - expect(tally([1, 2, 2, 3, 3, 3])).toEqual({ - 1: 1, - 2: 2, - 3: 3, - }); -}); - -// Given an invalid input like a string -// When passed to tally -// Then it should throw an error -test("throws an error when input is not an array", () => { - expect(() => tally("hello")).toThrow(); -}); From fe018ee13a399d5179256a4a25de9c19288dee49 Mon Sep 17 00:00:00 2001 From: mandip sanger Date: Mon, 10 Aug 2026 20:04:38 +0100 Subject: [PATCH 09/17] removing the test cases --- Sprint-1/implement/dedupe.js | 3 --- 1 file changed, 3 deletions(-) diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index b653a8e91..884881c16 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -3,6 +3,3 @@ function dedupe(arr) { } module.exports = dedupe; - -console.log(dedupe(["a", "a", "a", "b", "b", "c"])); // ['a', 'b', 'c'] -console.log(dedupe([5, 1, 1, 2, 3, 2, 5, 8])); // [5, 1, 2, 3, 8] From 9cd6eabfc5ba518da3684e605f7989408650f766 Mon Sep 17 00:00:00 2001 From: mandip sanger Date: Mon, 10 Aug 2026 20:05:57 +0100 Subject: [PATCH 10/17] dedupe implement fix --- Sprint-2/interpret/invert.js | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/Sprint-2/interpret/invert.js b/Sprint-2/interpret/invert.js index bb353fb1f..330f5e76c 100644 --- a/Sprint-2/interpret/invert.js +++ b/Sprint-2/interpret/invert.js @@ -10,20 +10,37 @@ function invert(obj) { const invertedObj = {}; for (const [key, value] of Object.entries(obj)) { - invertedObj.key = value; + invertedObj[value] = key; } return invertedObj; } -// a) What is the current return value when invert is called with { a : 1 } +module.exports = invert; -// b) What is the current return value when invert is called with { a: 1, b: 2 } +// a) What is the current return value when invert is called with { a : 1 } returns { key: 1 } +// b) What is the current return value when invert is called with { a: 1, b: 2 } = { key: 2 } // c) What is the target return value when invert is called with {a : 1, b: 2} -// c) What does Object.entries return? Why is it needed in this program? +// c) What does Object.entries return? Why is it needed in this program? Object.entries({ a: 1, b: 2 }); returns [ +//["a", 1], +//["b", 2] It is needed because it allows us to loop through each key and value: // d) Explain why the current return value is different from the target output - -// e) Fix the implementation of invert (and write tests to prove it's fixed!) +// //This creates a property literally called "key". +//For { a: 1, b: 2 }, it becomes: { key: 2 } + +// e) Fix the implementation of invertand write tests to prove it's fixed!) +//const invert = require("./invert.js"); + +// test("should invert an object", () => { +// expect(invert({ a: 1, b: 2 })).toEqual({ +// 1: "a", +// 2: "b", +// }); +// }); + +// test("should invert an empty object", () => { +// expect(invert({})).toEqual({}); +// }); From 2bb6031cc962dbcf2d7692845eb35b1a119cf38c Mon Sep 17 00:00:00 2001 From: mandip sanger Date: Mon, 10 Aug 2026 20:12:49 +0100 Subject: [PATCH 11/17] replacing invert file --- Sprint-2/interpret/invert.js | 29 ++++++----------------------- 1 file changed, 6 insertions(+), 23 deletions(-) diff --git a/Sprint-2/interpret/invert.js b/Sprint-2/interpret/invert.js index 330f5e76c..bb353fb1f 100644 --- a/Sprint-2/interpret/invert.js +++ b/Sprint-2/interpret/invert.js @@ -10,37 +10,20 @@ function invert(obj) { const invertedObj = {}; for (const [key, value] of Object.entries(obj)) { - invertedObj[value] = key; + invertedObj.key = value; } return invertedObj; } -module.exports = invert; +// a) What is the current return value when invert is called with { a : 1 } -// a) What is the current return value when invert is called with { a : 1 } returns { key: 1 } +// b) What is the current return value when invert is called with { a: 1, b: 2 } -// b) What is the current return value when invert is called with { a: 1, b: 2 } = { key: 2 } // c) What is the target return value when invert is called with {a : 1, b: 2} -// c) What does Object.entries return? Why is it needed in this program? Object.entries({ a: 1, b: 2 }); returns [ -//["a", 1], -//["b", 2] It is needed because it allows us to loop through each key and value: +// c) What does Object.entries return? Why is it needed in this program? // d) Explain why the current return value is different from the target output -// //This creates a property literally called "key". -//For { a: 1, b: 2 }, it becomes: { key: 2 } - -// e) Fix the implementation of invertand write tests to prove it's fixed!) -//const invert = require("./invert.js"); - -// test("should invert an object", () => { -// expect(invert({ a: 1, b: 2 })).toEqual({ -// 1: "a", -// 2: "b", -// }); -// }); - -// test("should invert an empty object", () => { -// expect(invert({})).toEqual({}); -// }); + +// e) Fix the implementation of invert (and write tests to prove it's fixed!) From 0aa32812b93ec385378ea740db1b5911487a9cc1 Mon Sep 17 00:00:00 2001 From: mandip sanger Date: Tue, 15 Sep 2026 13:26:00 +0100 Subject: [PATCH 12/17] median.js throw error --- Sprint-1/fix/median.js | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index c58e757a7..123b91fae 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -5,20 +5,33 @@ // Hint: Please consider scenarios when 'list' doesn't have numbers (the function is expected to return null) // or 'list' has mixed values (the function is expected to sort only numbers). function calculateMedian(list) { - list = list.filter((item) => typeof item === "number" && !isNaN(item)); + // Check that list is an array + if (!Array.isArray(list)) { + throw new Error("calculateMedian requires an array"); + } + // Empty array returns null if (list.length === 0) { return null; } + // Check that every item is a number + if (!list.every((item) => typeof item === "number" && !isNaN(item))) { + throw new Error("calculateMedian requires an array of numbers"); + } + + // Sort the numbers from smallest to largest list.sort((a, b) => a - b); + // Find the middle index const middleIndex = Math.floor(list.length / 2); + // Odd number of elements if (list.length % 2 === 1) { return list[middleIndex]; } + // Even number of elements return (list[middleIndex - 1] + list[middleIndex]) / 2; } From c8b160500c2e638f22549991d08e2c9c433185de Mon Sep 17 00:00:00 2001 From: mandip sanger Date: Tue, 15 Sep 2026 13:29:25 +0100 Subject: [PATCH 13/17] incoming changes --- Sprint-1/fix/median.js | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index 123b91fae..59db223aa 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -4,15 +4,16 @@ // Hint: Please consider scenarios when 'list' doesn't have numbers (the function is expected to return null) // or 'list' has mixed values (the function is expected to sort only numbers). + function calculateMedian(list) { - // Check that list is an array + // Check that the input is an array if (!Array.isArray(list)) { - throw new Error("calculateMedian requires an array"); + throw new Error("calculateMedian requires an array of numbers"); } - // Empty array returns null + // Check that the array is not empty if (list.length === 0) { - return null; + throw new Error("calculateMedian requires a non-empty array"); } // Check that every item is a number @@ -20,19 +21,22 @@ function calculateMedian(list) { throw new Error("calculateMedian requires an array of numbers"); } - // Sort the numbers from smallest to largest - list.sort((a, b) => a - b); + // Make a copy so we don't modify the original array + const sortedList = [...list]; + + // Sort numbers from smallest to largest + sortedList.sort((a, b) => a - b); // Find the middle index - const middleIndex = Math.floor(list.length / 2); + const middleIndex = Math.floor(sortedList.length / 2); // Odd number of elements - if (list.length % 2 === 1) { - return list[middleIndex]; + if (sortedList.length % 2 === 1) { + return sortedList[middleIndex]; } // Even number of elements - return (list[middleIndex - 1] + list[middleIndex]) / 2; + return (sortedList[middleIndex - 1] + sortedList[middleIndex]) / 2; } module.exports = calculateMedian; From 6f43ab5f1332ff3d74810338082dd013457ae42c Mon Sep 17 00:00:00 2001 From: mandip sanger Date: Tue, 15 Sep 2026 13:35:17 +0100 Subject: [PATCH 14/17] acceipt new changes --- Sprint-1/implement/max.test.js | 48 ++++++++++++++++++++-------------- 1 file changed, 28 insertions(+), 20 deletions(-) diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index 1176fb547..ad8ff0e0e 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -15,8 +15,6 @@ const findMax = require("./max.js"); // Given an empty array // When passed to the findMax function // Then it should return -Infinity -// Delete this test.todo and replace it with a test. - test("returns -Infinity for an empty array", () => { const currentOutput = findMax([]); const targetOutput = -Infinity; @@ -27,55 +25,65 @@ test("returns -Infinity for an empty array", () => { // Given an array with one number // When passed to the findMax function // Then it should return that number - -test("returns with one number", () => { +test("returns the number when the array contains one number", () => { const currentOutput = findMax([3]); const targetOutput = 3; + expect(currentOutput).toEqual(targetOutput); }); // Given an array with both positive and negative numbers // When passed to the findMax function // Then it should return the largest number overall -test("returns largest number", () => { +test("returns the largest number", () => { const currentOutput = findMax([3, -10]); const targetOutput = 3; + expect(currentOutput).toEqual(targetOutput); }); // Given an array with just negative numbers // When passed to the findMax function // Then it should return the closest one to zero -test("return closest to 0", () => { +test("returns the number closest to zero", () => { const currentOutput = findMax([-7, -3, -10]); const targetOutput = -3; + expect(currentOutput).toEqual(targetOutput); }); // Given an array with decimal numbers // When passed to the findMax function // Then it should return the largest decimal number -test("return to largest decimal number", () => { +test("returns the largest decimal number", () => { const currentOutput = findMax([2.5, 5.6]); const targetOutput = 5.6; + expect(currentOutput).toEqual(targetOutput); }); -// Given an array with non-number values +// Given an array containing a value that isn't a number // When passed to the findMax function -// Then it should return the findMax and ignore non-numeric values - -test("return to findMax and ignore non-numeric values", () => { - const currentOutput = findMax([7, 4, -10, "b", "f"]); - const targetOutput = 7; - expect(currentOutput).toEqual(targetOutput); +// Then it should throw Error("findMax requires an array of numbers") +test("throws an error when the array contains a non-number", () => { + expect(() => findMax(["hey", 10, "hi", 60, 10])).toThrow( + new Error("findMax requires an array of numbers") + ); }); -// Given an array with only non-number values +// Given something that isn't an array at all // When passed to the findMax function -// Then it should return the least surprising value given how it behaves for all other inputs -test("return the least surprising value", () => { - const currentOutput = findMax([NaN, "hi", true]); - const targetOutput = -Infinity; - expect(currentOutput).toEqual(targetOutput); +// Then it should throw Error("findMax requires an array of numbers") +test("throws an error when the input is not an array", () => { + expect(() => findMax("hey")).toThrow( + new Error("findMax requires an array of numbers") + ); + + expect(() => findMax(42)).toThrow( + new Error("findMax requires an array of numbers") + ); + + expect(() => findMax()).toThrow( + new Error("findMax requires an array of numbers") + ); }); From 19b40af4c83327b927bef0a74dca518c14faa95e Mon Sep 17 00:00:00 2001 From: mandip sanger Date: Tue, 15 Sep 2026 13:41:55 +0100 Subject: [PATCH 15/17] accepted incoming changes --- Sprint-1/implement/max.js | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 9de46d2b6..1877434f7 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,13 +1,13 @@ -function findMax(elements) { - const numbersOnly = elements.filter( - (item) => typeof item === "number" && !Number.isNaN(item) - ); - if (numbersOnly.length === 0) { - return -Infinity; +function findMax(list) { + if (!Array.isArray(list)) { + throw new Error("findMax requires an array of numbers"); } - const sorted = [...numbersOnly].sort((a, b) => a - b); - const max = sorted[sorted.length - 1]; - return max; + + if (!list.every((item) => typeof item === "number" && !Number.isNaN(item))) { + throw new Error("findMax requires an array of numbers"); + } + + return Math.max(...list); } module.exports = findMax; From 435945682e0bad00612b29fa13be49513d97c3ee Mon Sep 17 00:00:00 2001 From: mandip sanger Date: Tue, 15 Sep 2026 13:41:59 +0100 Subject: [PATCH 16/17] accepted incoming changes for sum.js and sum.test.js --- Sprint-1/implement/sum.js | 14 +++---- Sprint-1/implement/sum.test.js | 67 +++++++++++++++------------------- 2 files changed, 36 insertions(+), 45 deletions(-) diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index d22a19af5..a19b87715 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,13 +1,13 @@ -function sum(elements) { - let total = 0; +function sum(list) { + if (!Array.isArray(list)) { + throw new Error("sum requires an array of numbers"); + } - for (let i = 0; i < elements.length; i++) { - if (typeof elements[i] === "number" && !isNaN(elements[i])) { - total += elements[i]; - } + if (!list.every((item) => typeof item === "number" && !Number.isNaN(item))) { + throw new Error("sum requires an array of numbers"); } - return total; + return list.reduce((total, number) => total + number, 0); } module.exports = sum; diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index 7287551f0..ca1f80285 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -1,18 +1,9 @@ -/* Sum the numbers in an array - -In this kata, you will need to implement a function that sums the numerical elements of an array - -E.g. sum([10, 20, 30]), target output: 60 -E.g. sum(['hey', 10, 'hi', 60, 10]), target output: 80 (ignore any non-numerical elements) -*/ - const sum = require("./sum.js"); -// Acceptance Criteria: + // Given an empty array // When passed to the sum function // Then it should return 0 - -test("returns 0 for empty array", () => { +test("returns 0 for an empty array", () => { const list = []; const currentOutput = sum(list); const targetOutput = 0; @@ -23,54 +14,54 @@ test("returns 0 for empty array", () => { // Given an array with just one number // When passed to the sum function // Then it should return that number - -test("sum of 1 number", () => { +test("returns the number when the array contains one number", () => { const list = [1]; const currentOutput = sum(list); - const targetOutput = 1; + expect(currentOutput).toEqual(targetOutput); }); + // Given an array containing negative numbers // When passed to the sum function // Then it should still return the correct total sum - -test("sum of negative number in arrays", () => { - const list = [-5]; +test("returns the correct sum with negative numbers", () => { + const list = [-5, -10, 3]; const currentOutput = sum(list); - const targetOutput = -5; + const targetOutput = -12; + expect(currentOutput).toEqual(targetOutput); }); // Given an array with decimal/float numbers // When passed to the sum function // Then it should return the correct total sum - -test("sum of decimal/float number in arrays", () => { - const list = [4.5, 2.5]; +test("returns the correct sum with decimal numbers", () => { + const list = [2.5, 3.5, 4]; const currentOutput = sum(list); - const targetOutput = 7; + const targetOutput = 10; + expect(currentOutput).toEqual(targetOutput); }); -// Given an array containing non-number values +// Given an array containing a value that isn't a number // When passed to the sum function -// Then it should ignore the non-numerical values and return the sum of the numerical elements -test("ignores non-number values", () => { - const list = ["hey", 10, "hi", 60, 10]; - const currentOutput = sum(list); - const targetOutput = 80; - - expect(currentOutput).toEqual(targetOutput); +// Then it should throw Error("sum requires an array of numbers") +test("throws an error when the array contains a non-number", () => { + expect(() => sum(["hey", 10, "hi", 60, 10])).toThrow( + new Error("sum requires an array of numbers") + ); }); -test("ignores NaN values when calculating the sum", () => { - const list = [4, NaN, 6]; - const currentOutput = sum(list); - const targetOutput = 10; +// Given something that isn't an array at all +// When passed to the sum function +// Then it should throw Error("sum requires an array of numbers") +test("throws an error when the input is not an array", () => { + expect(() => sum("hey")).toThrow( + new Error("sum requires an array of numbers") + ); - expect(currentOutput).toEqual(targetOutput); + expect(() => sum(42)).toThrow(new Error("sum requires an array of numbers")); + + expect(() => sum()).toThrow(new Error("sum requires an array of numbers")); }); -// 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 From abc979e24ef8fed8598bec02b5e60351028a63db Mon Sep 17 00:00:00 2001 From: mandip sanger Date: Tue, 15 Sep 2026 23:32:05 +0100 Subject: [PATCH 17/17] sorting median.js --- Sprint-1/fix/median.js | 48 +++--------------------------------------- 1 file changed, 3 insertions(+), 45 deletions(-) diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index 22eea0e04..5c5b796e1 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -6,51 +6,9 @@ // or contains values that aren't numbers (the function is expected to throw - see the tests). function calculateMedian(list) { - // Check that the input is an array - if (!Array.isArray(list)) { - throw new Error("calculateMedian requires an array of numbers"); - } - - // Check that the array is not empty - if (list.length === 0) { - throw new Error("calculateMedian requires a non-empty array"); - } - - // Check that every item is a number - if (!list.every((item) => typeof item === "number" && !isNaN(item))) { - throw new Error("calculateMedian requires an array of numbers"); - } - - // Make a copy so we don't modify the original array - const sortedList = [...list]; - - // Sort numbers from smallest to largest - sortedList.sort((a, b) => a - b); - - // Find the middle index - const middleIndex = Math.floor(sortedList.length / 2); - - // Odd number of elements - if (sortedList.length % 2 === 1) { - return sortedList[middleIndex]; - } - - // Even number of elements - return (sortedList[middleIndex - 1] + sortedList[middleIndex]) / 2; + const middleIndex = Math.floor(list.length / 2); + const median = list.splice(middleIndex, 1)[0]; + return median; } module.exports = calculateMedian; - -// function calculateMedian(list) { -// if (list.length === 0) return null; -// list = list.filter((item) => typeof item === "number" && !isNaN(item)); -// list.sort((a, b) => a - b); -// const middleIndex = Math.floor(list.length / 2); -// if (list.length % 2 === 1) { -// return list[middleIndex]; //odd number if elements -// } - -// return (list[middleIndex - 1] + list[middleIndex]) / 2; -// } - -// module.exports = calculateMedian;