From e969dec04a98ddcd0135259e772058443ab85b9c Mon Sep 17 00:00:00 2001 From: HannaOdud Date: Wed, 5 Nov 2025 21:59:47 +0000 Subject: [PATCH] Sprint-1 --- Sprint-1/fix/median.js | 19 +++++++++++++--- Sprint-1/implement/dedupe.js | 11 ++++++++- Sprint-1/implement/dedupe.test.js | 21 ++++++++++++++++-- Sprint-1/implement/max.js | 15 +++++++++++++ Sprint-1/implement/max.test.js | 37 ++++++++++++++++++++++++++++++- Sprint-1/implement/sum.js | 17 ++++++++++++++ Sprint-1/implement/sum.test.js | 34 ++++++++++++++++++++++++++-- Sprint-1/refactor/includes.js | 5 ++--- 8 files changed, 147 insertions(+), 12 deletions(-) diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index b22590bc6..4ee14115e 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -6,9 +6,22 @@ // or 'list' has mixed values (the function is expected to sort only numbers). function calculateMedian(list) { - const middleIndex = Math.floor(list.length / 2); - const median = list.splice(middleIndex, 1)[0]; - return median; + if (!Array.isArray(list)){ //check if variable is array + return null; + } + const numbersOnly = list.filter(item => typeof item === "number" && !isNaN(item)); + if (numbersOnly.length == 0){ + return null; + } + const sorted =[...numbersOnly].sort((a,b) => a-b ); //we created a new arrey where we saved sorted element (as string) and turned them to numbers. + + const middleIndex = Math.floor(sorted.length / 2); + if (sorted.length % 2 !== 0){ //for odds length of arrey + return sorted[middleIndex] + } + else{ + return (sorted[middleIndex -1] + sorted[middleIndex]) / 2; //for even length of arrey + } } module.exports = calculateMedian; diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 781e8718a..5a80d5567 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1 +1,10 @@ -function dedupe() {} +function dedupe(array) { + if (array.length == 0){ // check if array is empty + return []; + } + let mySet = new Set(array); // create set mySet + let dedupeArray = Array.from (mySet); // create new array from the set mySet + return dedupeArray; +} + +module.exports = dedupe; \ No newline at end of file diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index 23e0f8638..7448f3f23 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"); +test("given an empty array, it returns an empty array", () => { + const currentOutput = dedupe([]); + const targetOutput = []; + expect(currentOutput).toEqual(targetOutput); +}); // Given an array with no duplicates // When passed to the dedupe function // Then it should return a copy of the original array - +test("given an array with no duplicates, it should return a copy of the original array", () => { + const currentOutput = dedupe([5, 1, 2, 3, 8]); + const targetOutput = [5, 1, 2, 3, 8]; + expect(currentOutput).toEqual(targetOutput); +}); // 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 +test("given an array with strings or number, it should remove the duplicate values, preserving the first occurence of each element", () => { + const currentOutput = dedupe([5, "1", "1", 5, 8]); + const targetOutput = [5, "1", 8]; + + console.log(currentOutput); + console.log(targetOutput); + + expect(currentOutput).toEqual(targetOutput); +}); \ No newline at end of file diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 6dd76378e..4eb03bda4 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,4 +1,19 @@ function findMax(elements) { + //filter array for numbersOnly + const numbersOnly = elements.filter(item => !isNaN(item)); + if (numbersOnly.length == 0){ // check if array is empty + return -Infinity; + } + if (numbersOnly.length === 1){ //check if they has one element + return numbersOnly[0]; + } + let max = numbersOnly[0];// find a max number + for (let i=1; 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..87aede035 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -16,28 +16,63 @@ 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, it should return -Infinity", () => { + const currentOutput = findMax([]); + 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("Given an array with one number, it should return that number", () => { + const currentOutput = findMax([1]); + const targetOutput = 1; + 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("Given an array with both positive and negative numbers it should return the largest number overall", () => { + const currentOutput = findMax([-2, 2, 3, -3]); + 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("Given an array with just negative numbers, it should returnthe closest one to zero", () => { + const currentOutput = findMax([-1, -2, -3, -4]); + const targetOutput = -1; + 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("given an array with decimal numbers, it should return the largest decimal number", () => { + const currentOutput = findMax([0.2, 0.45, 5.3, 1.5]); + const targetOutput = 5.3; + 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("given an array with non-number values, it should return the max and ignore non-numeric values", () => { + const currentOutput = findMax(["a", "!", 3, 5, "0"]); + const targetOutput = 5; + 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("given an array with only non-number values, it should return the least surprising value given how it behaves for all other inputs", () => { + const currentOutput = findMax(["2", "3", "$", "%"]); + const targetOutput = "3"; + expect(currentOutput).toEqual(targetOutput); +}); \ No newline at end of file diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index 9062aafe3..0519f1ea9 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,4 +1,21 @@ function sum(elements) { + if (elements.length == 0){ // check if array is empty + return 0; + } + if (elements.length === 1){ //check if array has one element + return elements[0]; + } + let sum = 0; + let isNumbersPresent = elements.some(item => typeof item === 'number' && !isNaN(item)); + for (let i =0; i { + const currentOutput = sum ([]); + 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("given an array with just one number, it should return that number", () => { + const currentOutput = sum ([1]); + 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("given an array containing negative numbers, it should still return the correct total sum", () => { + const currentOutput = sum ([-2, -3, -4, -5]); + const targetOutput = -14; + 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("given an array with decimal/float numbers, it should return the correct total sum", () => { + const currentOutput = sum ([2.5, 3.1, 4.9]); + const targetOutput = 10.5; + expect(currentOutput).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("given an array containing non-number values, it should return the sum of the numerical elements", () => { + const currentOutput = sum (["a", "!", 3, 5, ","]); + const targetOutput = 8; + 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 +test("given an array with only non-number values, it should return the least surprising value given how it behaves for all other inputs", () => { + const currentOutput = sum (["a", "!", "3", "5", ","]); + const targetOutput = 8; + expect(currentOutput).toEqual(targetOutput); +}); \ No newline at end of file diff --git a/Sprint-1/refactor/includes.js b/Sprint-1/refactor/includes.js index 29dad81f0..965e9175f 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 element of list){ + if (element === target) { return true; } }