diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index b22590bc6..4507226c4 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -6,9 +6,35 @@ // 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; + // Check that the input passed to the function is an array + if (!Array.isArray(list)) { + return null; + } + + // Filter valid numbers and store them in `validNumbers` + const validNumbers = list.filter( + (item) => typeof item === "number" && !isNaN(item) + ); + + // Return `null` if there are no valid numbers + if (validNumbers.length === 0) { + return null; + } + + // Sort `validNumbers` in ascending order + // Sort validNumbers (safe to mutate because it's a new array created by filter) + validNumbers.sort((a, b) => a - b); + + // Calculate the middle index of the sorted array + const middleIndex = Math.floor(validNumbers.length / 2); + + // If the array has an odd number of elements, return the middle element + if (validNumbers.length % 2 !== 0) { + return validNumbers[middleIndex]; + } else { + // If the array has an even number of elements, return the average of the two middle elements + return (validNumbers[middleIndex - 1] + validNumbers[middleIndex]) / 2; + } } module.exports = calculateMedian; 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..b7372b6e7 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1 +1,43 @@ -function dedupe() {} +// This defines a function named dedupe with a parameter arrayInput. arrayInput should be an array. +function dedupe(arrayInput) { + // This checks whether the input is not an array. + if (!Array.isArray(arrayInput)) { + // If the input is not an array, the function stops, throws a TypeError and shows an error message saying:“Input must be an array.” + throw new TypeError("Input must be an array"); + } + // This checks if the array is empty (has no elements). + if (arrayInput.length === 0) { + // If the array is empty, it returns an empty array. + return []; + } + + // This declares a new variable `newArray` which will hold a reference to an empty array in memory + const newArray = []; + // This decares a new varibale `seen` which will hold a reference to the actual set object in memory + const seen = new Set(); // variable seen keeps track of all items that have already been added to the result array + // seen is a Set object that remembers items, + + // newArray is an array that holds the actual result. + + // This starts a loop that goes through each element in the input array one by one. + for (const item of arrayInput) { + // This big condition checks two things before adding an item: + + // !seen.has(item) — The item has not been added before. + + // The item is either a number (but not NaN) or a string. + // So only real numbers and strings that haven’t appeared yet will be kept. + if ( + !seen.has(item) && + ((typeof item === "number" && !Number.isNaN(item)) || + typeof item === "string") + ) { + newArray.push(item); // If the item passes the condition, it gets added to the new array. + seen.add(item); // The item is then recorded in the Set, so it won’t be added again later. + } + } + // After the loop finishes, return the final deduplicated array + return newArray; // After checking all items, the function returns the new array with duplicates removed and invalid items filtered out. +} + +module.exports = dedupe; // This line allows the dedupe function to be used in other files (like the test file). diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index 23e0f8638..41fc280d5 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -1,10 +1,11 @@ const dedupe = require("./dedupe.js"); + /* Dedupe Array -📖 Dedupe means **deduplicate** +📖 Dedupe means **dedupe** -In this kata, you will need to deduplicate the elements of an array +In this kata, you will need to dedupe the elements of an array E.g. dedupe(['a','a','a','b','b','c']) target output: ['a','b','c'] E.g. dedupe([5, 1, 1, 2, 3, 2, 5, 8]) target output: [5, 1, 2, 3, 8] @@ -13,15 +14,80 @@ E.g. dedupe([1, 2, 1]) target output: [1, 2] // Acceptance Criteria: -// 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 - valid inputs", () => { + // Given an empty array + // When passed to the dedupe function + // Then it should return an empty array + [{ input: [], expected: [] }].forEach(({ input, expected }) => + test(`returns an empty array for input [${input}]`, () => { + const result = dedupe(input); + expect(result).toEqual(expected); + expect(result).not.toBe(input); // ensures it's a new array + }) + ); + + // 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: ["a", "b", "car"], expected: ["a", "b", "car"] }, + { input: [0, 1, 5, 6], expected: [0, 1, 5, 6] }, + ].forEach(({ input, expected }) => + test(`returns a copy of the array when no duplicates exist for input [${input}]`, () => { + const result = dedupe(input); + expect(result).toEqual(expected); + expect(result).not.toBe(input); // ensures it's a new array + }) + ); + + // Given arrays with duplicates + [ + { input: [5, 1, 1, 2, 3, 2, 5, 8], expected: [5, 1, 2, 3, 8] }, + { input: [1, 1, 1, 1], expected: [1] }, + { input: ["a", "a", "a", "b", "b", "c"], expected: ["a", "b", "c"] }, + ].forEach(({ input, expected }) => + test(`removes duplicate elements and keeps the first occurrence for [${input}]`, () => { + const result = dedupe(input); + expect(result).toEqual(expected); + expect(result).not.toBe(input); // ensures it's a new array + }) + ); + + // Given arrays with numbers and strings mixed + [ + { input: ["1", 1, "1", 1], expected: ["1", 1] }, + { input: [2, "3", "hello", 5, 5], expected: [2, "3", "hello", 5] }, + ].forEach(({ input, expected }) => + test(`removes duplicates from an array of numbers and strings, keeping the first occurrence for input [${input}]`, () => { + const result = dedupe(input); + expect(result).toEqual(expected); + expect(result).not.toBe(input); // ensures it's a new array + }) + ); + + // Given arrays with invalid elements + [ + { input: [1, 2, true, 3, null, "a", {}], expected: [1, 2, 3, "a"] }, + { input: ["x", undefined, "x", "y", []], expected: ["x", "y"] }, + { input: [false, 5, 5, "hello", () => {}, 5], expected: [5, "hello"] }, + { input: [1, NaN, 2, NaN, "hello"], expected: [1, 2, "hello"] }, + ].forEach(({ input, expected }) => + test(`filters invalid elements, removes duplicates, and returns a copy for input [${input}]`, () => { + const result = dedupe(input); + expect(result).toEqual(expected); + expect(result).not.toBe(input); // ensures it's a new array + }) + ); +}); -// Given an array with no duplicates -// When passed to the dedupe function -// Then it should return a copy of the original array +describe("dedupe - invalid inputs", () => { + const invalidInputs = [null, undefined, 123, "string", {}, () => {}]; -// 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 + invalidInputs.forEach((input) => + test(`throws typeError when input is ${String(input)}`, () => { + expect(() => dedupe(input)).toThrow(TypeError); + expect(() => dedupe(input)).toThrow("Input must be an array"); + }) + ); +}); diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 6dd76378e..0e06ebe78 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,4 +1,12 @@ function findMax(elements) { + if (!Array.isArray(elements)) { + throw new TypeError("Input must be an array"); + } else { + const filteredArray = elements.filter( + (ele) => typeof ele === "number" && !isNaN(ele) + ) + return Math.max(...filteredArray); + } } module.exports = findMax; diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index 82f18fd88..8ba3d5fd3 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -16,28 +16,76 @@ 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([8])).toBe(8); + expect(findMax([-42])).toBe(-42); + expect(findMax([0])).toBe(0); +}); // 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 both positive and negative numbers, returns the largest number overall", () => { + expect(findMax([10, -5, 20, 0])).toBe(20); + expect(findMax([-1, -100, -50, 30])).toBe(30); + expect(findMax([0, -10, 5])).toBe(5); +}); // 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 just negative numbers, returns the closest one to zero", () => { + expect(findMax([-10, -5, -20])).toBe(-5); + expect(findMax([-100, -50, -60])).toBe(-50); + expect(findMax([-1000, -60, -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 array with decimal numbers, returns the largest decimal number", () => { + expect(findMax([1.1, 2.5, 0.3])).toBe(2.5); + expect(findMax([-1.2, -0.5, -0.1])).toBe(-0.1); + expect(findMax([-1000.23, -80.54, -63.110])).toBe(-63.110); +}); // 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 the non-numeric values", () => { + expect(findMax([10, "hi", 20, null])).toBe(20); + expect(findMax([0, false, 5, "a"])).toBe(5); + expect(findMax([undefined, 5, null, NaN, -9])).toBe(5); +}); // 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", () => { + expect(findMax(["a", null, undefined])).toBe(-Infinity); + expect(findMax([{}, [], "hi"])).toBe(-Infinity); + expect(findMax([true, false, true])).toBe(-Infinity); + expect(findMax([Symbol("x"), () => {}, function () {}])).toBe(-Infinity); +}); + +// Edge cases +// Given an array with non-array values +// when passed to the max function +// Then it should throw and error +test("given a non-array input, throws a TypeError with message 'Input must be an array'", () => { + expect(() => findMax(42)).toThrow(new TypeError("Input must be an array")); + expect(() => findMax("hello")).toThrow( + new TypeError("Input must be an array") + ); + expect(() => findMax(null)).toThrow(new TypeError("Input must be an array")); + expect(() => findMax(undefined)).toThrow( + new TypeError("Input must be an array") + ); +}); diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index 9062aafe3..721239868 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,4 +1,16 @@ function sum(elements) { + if (!Array.isArray(elements)) { + throw new TypeError("Input must be an array"); + } + const filteredElements = elements.filter( + (ele) => typeof ele === "number" && !isNaN(ele) + ); + let sumElements = 0; + + for (const item of filteredElements) { + sumElements += item; + } + return sumElements; } module.exports = sum; diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index dd0a090ca..dc4748e3a 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -13,24 +13,54 @@ 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); + + const arr = []; + expect(sum(arr)).toBe(0); +}); // Given an array with just one number // When passed to the sum function // Then it should return that number +test("given one number in an array, returns that number", () => { + expect(sum([5])).toBe(5); + expect(sum([-3])).toBe(-3); + expect(sum([0])).toBe(0); +}); // 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, should return the correct total sum", () => { + expect(sum([-5, -9, -100])).toBe(-114); + expect(sum([-1000, -2000, -3000])).toBe(-6000); + expect(sum([-1, -2, -3])).toBe(-6); +}); // 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, should return the correct total number", () => { + expect(sum([-8.24, 5.46, 3.22])).toBeCloseTo(0.44); + expect(sum([-2.5, 2.5, 5.25])).toBeCloseTo(5.25); + expect(sum([1.11, 2.22, 3.33])).toBeCloseTo(6.66); +}); // 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, should ignore the non-numerical values and return the sum of the numerical elements", () => { + expect(sum([5, -18, 5.601, null, "kettle"])).toBeCloseTo(-7.4); + expect(sum([true, false, 10, -5])).toBe(5); + expect(sum([null, 4, 6, "test"])).toBe(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 +test("given an array with only non-number values, should return 0", () => { + expect(sum(["food", null, false, true])).toBe(0); + expect(sum(["apple", "banana", null, undefined])).toBe(0); + expect(sum([NaN, NaN, NaN])).toBe(0); +}); diff --git a/Sprint-1/refactor/includes.js b/Sprint-1/refactor/includes.js index 29dad81f0..6aeac0b5d 100644 --- a/Sprint-1/refactor/includes.js +++ b/Sprint-1/refactor/includes.js @@ -1,9 +1,8 @@ // 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) { + for (const item of list) { + if (item === target) { return true; } } diff --git a/prep/mean-median.js b/prep/mean-median.js new file mode 100644 index 000000000..c79e65573 --- /dev/null +++ b/prep/mean-median.js @@ -0,0 +1,53 @@ +function calculateMean(list) { + let total = 0; + + for (const item of list) { + total += item; + } + console.log("the total of the array is", total); + console.log("the element has",list.length, "elements"); + return (total/list.length); + +} + +function calculateMedian(list) { + const middleIndex = Math.floor(list.length / 2); + const median = list.splice(middleIndex, 1)[0]; // Because the splice method has been used on the variable list, + // the array stored in memory will change, and any variable that points to it will show the updated array. + + return median; +} + +const salaries = [10, 20, 30, 40, 60, 80, 80]; +const median = calculateMedian(salaries); + +console.log(salaries, "<--- salaries input before we call calculateMean"); +const mean = calculateMean(salaries); + +console.log(`The median of the salaries is ${median}`); +console.log(`The mean of the salaries is ${mean}`); +console.log(salaries); + +// Notes to understand Array as a reference type +// 1. Arrays are reference types + +// This means that when you create an array like this: +// const salaries = [10, 20, 30, 40, 60, 80, 80]; +// JavaScript doesn’t store the array itself directly in the variable salaries. + +// Instead, the array is stored somewhere in memory, and salaries holds a reference (or pointer) to that memory location. +// 2. What “reference” means + +// Think of it like this: the array is a house, and salaries is the address of that house. + +// If you write: +// const other = salaries; +// now other has the same address. Both salaries and other point to the same array in memory. + +// 3. Implication: modifying the array + +// Because both variables point to the same array, changing it through one variable affects the other: +// other[0] = 100; +// console.log(salaries[0]); // 100 +// This happens because there is only one array in memory, and both variables reference it. + diff --git a/prep/mean.js b/prep/mean.js new file mode 100644 index 000000000..de9d6f4d7 --- /dev/null +++ b/prep/mean.js @@ -0,0 +1,14 @@ +function calculateMean(list) { + let total = 0; + + for (const item of list) { + total += item; + } + console.log("the total of the array is", total); + console.log("the element has", list.length, "elements"); + return total / list.length; +} + +//calculateMean([1,2,3,4,5]); + +module.exports = calculateMean; \ No newline at end of file diff --git a/prep/mean.test.js b/prep/mean.test.js new file mode 100644 index 000000000..4123bf07e --- /dev/null +++ b/prep/mean.test.js @@ -0,0 +1,9 @@ +const calculateMean = require("./mean"); + +test("Calculate the average of a list of numbers", () => { + const list = [3, 50, 7]; + const currentOutput = calculateMean(list); + const targetOutput = 20; // 20 is (3 + 50 + 7) / 3 + + expect(currentOutput).toEqual(targetOutput); +}); diff --git a/prep/median.js b/prep/median.js new file mode 100644 index 000000000..d008917bb --- /dev/null +++ b/prep/median.js @@ -0,0 +1,31 @@ +// How to calculate the median of an array by mutating the array + +// function calculateMedian(list) { +// const middleIndex = Math.floor(list.length / 2); +// const median = list.splice(middleIndex, 1)[0]; +// console.log("middleIndex", middleIndex); +// console.log(list); +// return median; +// } + +// const actualOutput = calculateMedian([1, 2, 3]); +// console.log("median", actualOutput); +module.exports = calculateMedian; + +// How to calculate the median of an array without muatating the array +function calculateMedian(list) { + let medianIndex; + let actualMedian; + const arrayLength = list.length; // 3 + if (arrayLength % 2 !== 0) { + medianIndex = (arrayLength + 1) / 2; + actualMedian = list[medianIndex - 1]; + } else { + medianIndex = arrayLength / 2; + + firstMedianIndex = list[medianIndex - 1]; + secondMedianIndex = list[medianIndex]; + actualMedian = (firstMedianIndex + secondMedianIndex) / 2; + } + return actualMedian; +} diff --git a/prep/median.test.js b/prep/median.test.js new file mode 100644 index 000000000..b0ed64d2d --- /dev/null +++ b/prep/median.test.js @@ -0,0 +1,24 @@ +const calculateMedian = require("./median"); + +test("Calculate the median of the list of numbers", () => { + const list = [10, 20, 30, 40, 50]; + const currentOutput = calculateMedian(list); + const targetOutput = 30; + + expect(currentOutput).toEqual(targetOutput); +}); + +test("should not modify the input", () => { + const list = [1, 2, 3]; + const currentOutput = calculateMedian(list); + + expect(currentOutput).toEqual(2); +}); + +test("should return the average of the two middle numbers as the median for an array with even length", () => { + const list = [2, 6, 4, 3]; + const currentOutput = calculateMedian(list); + expect(currentOutput).toEqual(5) + } +); + diff --git a/prep/package.json b/prep/package.json new file mode 100644 index 000000000..8cf1fc5ab --- /dev/null +++ b/prep/package.json @@ -0,0 +1,13 @@ +{ + "devDependencies": { + "jest": "^30.2.0" + }, + "scripts": { + "test": "jest" + } +} + + + + +