Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
575c939
filtered and sorted a copy of the original list
alexandru-pocovnicu Feb 23, 2026
8ca70c3
Refactor calculateMedian function to handle non-array inputs and impr…
alexandru-pocovnicu Feb 24, 2026
0656e3b
Add test for findMax function to return Infinity for empty array
alexandru-pocovnicu Feb 24, 2026
b50166e
Implement findMax function to return the maximum number from an array…
alexandru-pocovnicu Feb 24, 2026
99d1421
Refactor findMax function and add test for handling both positive and…
alexandru-pocovnicu Feb 24, 2026
80d3cb0
Add test for findMax function to handle arrays with only negative num…
alexandru-pocovnicu Feb 24, 2026
ef647c6
Update tests for findMax function to include decimal numbers and adju…
alexandru-pocovnicu Feb 24, 2026
3db5d07
Fix findMax function to return -Infinity for empty arrays and update …
alexandru-pocovnicu Feb 24, 2026
b0a85a7
Update findMax function to filter out NaN values and add test for non…
alexandru-pocovnicu Feb 24, 2026
e2abc3f
Refactor findMax function and tests for improved readability and cons…
alexandru-pocovnicu Feb 24, 2026
573a325
Implement sum function and tests to handle various input scenarios, i…
alexandru-pocovnicu Feb 24, 2026
a5a261e
added export module
alexandru-pocovnicu Feb 24, 2026
d0a50ce
added parameter to dedupe
alexandru-pocovnicu Feb 24, 2026
6769952
Add test case for dedupe function to handle empty array
alexandru-pocovnicu Feb 24, 2026
c10a14f
Add early return for empty array in dedupe function
alexandru-pocovnicu Feb 24, 2026
efbcbb5
Add dedupe function logic and test case for arrays without duplicates
alexandru-pocovnicu Feb 24, 2026
d5e5142
Refactor dedupe function and add test case for handling duplicates in…
alexandru-pocovnicu Feb 24, 2026
498fa5f
Refactor includes function to use for...of loop for improved readability
alexandru-pocovnicu Feb 24, 2026
a8bb981
Refactor includes function to remove unnecessary whitespace for clean…
alexandru-pocovnicu Feb 24, 2026
30584f6
Add solution function to calculate the sum of an array of numbers
alexandru-pocovnicu Feb 24, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions Sprint-1/fix/median.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,29 @@
// 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;
}

let listCopy = [...list];
let filteredList = listCopy.filter((x) => typeof x === "number");
let sortedList = filteredList.sort((a, b) => a - b);
const middleIndex = Math.floor(sortedList.length / 2);

if (filteredList.length === 0) {
return null;
}

let median = 0;
if (sortedList.length % 2 === 0) {
median = (sortedList[middleIndex - 1] + sortedList[middleIndex]) / 2;
} else {
median = sortedList[middleIndex];
}

return median;
}



module.exports = calculateMedian;
19 changes: 18 additions & 1 deletion Sprint-1/implement/dedupe.js
Original file line number Diff line number Diff line change
@@ -1 +1,18 @@
function dedupe() {}
function dedupe(arr) {
if (arr.length === 0) {
return [];
}

for (let element of arr) {
if (arr.indexOf(element) === arr.lastIndexOf(element)) {
return arr;
} else {
arr.splice(arr.lastIndexOf(element), 1);
}
}
return arr;
}

console.log(dedupe([1, "d", 6, 1, 5, "d", null]));

module.exports = dedupe;
21 changes: 19 additions & 2 deletions Sprint-1/implement/dedupe.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const dedupe = require("./dedupe.js");
Dedupe Array

📖 Dedupe means **deduplicate**

In this kata, you will need to deduplicate the elements of an array

E.g. dedupe(['a','a','a','b','b','c']) target output: ['a','b','c']
Expand All @@ -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 array=[]
const currentOutput=dedupe(array)
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 returns a copy of the original array", () => {
const array = [1,"d",6];
const currentOutput = dedupe(array);
const targetOutput = [1,"d",6];
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 numbers, it removes the duplicate values, preserving the first occurence of each element", () => {
const array = [1, "d", 6,1,5,"d",null];
const currentOutput = dedupe(array);
const targetOutput = [1, "d", 6,5,null];
expect(currentOutput).toEqual(targetOutput);
});
7 changes: 7 additions & 0 deletions Sprint-1/implement/max.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
function findMax(elements) {
const filteredElements = elements.filter(
(x) => typeof x === "number" && !Number.isNaN(x)
);

const sortedElements = filteredElements.sort((a, b) => a - b);

return sortedElements.reduce((a, b) => Math.max(a, b), -Infinity);
}

module.exports = findMax;
43 changes: 42 additions & 1 deletion Sprint-1/implement/max.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,69 @@ 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", () => {
const array = [];
const currentOutput = findMax(array);
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, returns -that number", () => {
const array = [3];
const currentOutput = findMax(array);
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("given an array with both positive and negative numbers, returns -the largest number overall ", () => {
const array = [3.5, -5, 7, -9];
const currentOutput = findMax(array);
const targetOutput = 7;
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, return the closest one to 0", () => {
const array = [-5, -1, -7.6, -3];
const currentOutput = findMax(array);
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, return the largest decimal", () => {
const array = [-5.9, -1.8, -7.6, -3.2];
const currentOutput = findMax(array);
const targetOutput = -1.8;
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, returns the max number", () => {
const array = [-5, -1, "k", -7.6, null, -3];
const currentOutput = findMax(array);
const targetOutput = -1;
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-numbers, returns -Infinity", () => {
const array = [false, "k", "-7.6", null, {}, NaN];
const currentOutput = findMax(array);
const targetOutput = -Infinity;
expect(currentOutput).toEqual(targetOutput);
});
9 changes: 9 additions & 0 deletions Sprint-1/implement/sum.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
function sum(elements) {
const filteredElements = elements.filter(
(x) => typeof x === "number" && !Number.isNaN(x)
);

if (filteredElements.length === 0) {
return 0;
}
return filteredElements.reduce((a, b) => a + b);
}
console.log(sum([0]));

module.exports = sum;
39 changes: 37 additions & 2 deletions Sprint-1/implement/sum.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
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)
E.g. sum(['hey', 10, 'hi', 60, 10]), target output: 80 (ignore any non-numerical elements)
*/

const sum = require("./sum.js");
Expand All @@ -13,24 +13,59 @@ 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", () => {
const array = [];
const currentOutput = sum(array);
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, return the number", () => {
const array = [5];
const currentOutput = sum(array);
const targetOutput = 5;
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 containig negative numbers, return the total of the numbers", () => {
const array = [-5, -9, -3];
const currentOutput = sum(array);
const targetOutput = -17;
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 containig decimalnumbers, return the total of the numbers", () => {
const array = [-5.5, 9, -3];
const currentOutput = sum(array);
const targetOutput = 0.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 containig non-number values,ignore the non-numerical values and return the total of the numbers", () => {
const array = [5, "s", null, NaN, -9, -3];
const currentOutput = sum(array);
const targetOutput = -7;
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 containig only non-number values, return 0", () => {
const array = ["s", null, NaN, "-3", true];
const currentOutput = sum(array);
const targetOutput = 0;
expect(currentOutput).toEqual(targetOutput);
});
3 changes: 1 addition & 2 deletions Sprint-1/refactor/includes.js
Original file line number Diff line number Diff line change
@@ -1,8 +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];
for (let element of list) {
if (element === target) {
return true;
}
Expand Down
Loading