Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
71fa218
Add initial test file for mean calculations
alexandru-pocovnicu Oct 24, 2025
927769b
Add mean.js file for mean calculations
alexandru-pocovnicu Oct 24, 2025
a393af9
Remove mean.js and mean.test.js files
alexandru-pocovnicu Oct 26, 2025
057a7dd
Update dependencies in package-lock.json to latest versions
alexandru-pocovnicu Oct 27, 2025
bc5e43c
Refactor calculateMedian function to handle mixed values and improve …
alexandru-pocovnicu Oct 27, 2025
b13a731
Add workspace configuration for project structure and Jest settings
alexandru-pocovnicu Oct 27, 2025
552eb4c
Refactor calculateMedian function to filter non-numeric values and im…
alexandru-pocovnicu Oct 27, 2025
1e0393e
Refactor calculateMedian function to improve null checks and remove r…
alexandru-pocovnicu Oct 27, 2025
b78089c
Refactor calculateMedian function to streamline sorting and improve m…
alexandru-pocovnicu Oct 27, 2025
4f6b45e
Add tests for findMax function to cover various input scenarios
alexandru-pocovnicu Oct 27, 2025
5d670ec
Refactor findMax function to enhance readability and maintainability
alexandru-pocovnicu Oct 27, 2025
879eb0e
Implement tests for sum function to validate various input scenarios
alexandru-pocovnicu Oct 27, 2025
157b5f7
Implement sum function to calculate the sum of an array of numbers
alexandru-pocovnicu Oct 27, 2025
c85cfda
Implement tests for dedupe function to validate various input scenarios
alexandru-pocovnicu Oct 30, 2025
5619c22
Implement dedupe function to remove duplicate values from an array
alexandru-pocovnicu Oct 30, 2025
abd3a43
Format code for consistency and readability in dedupe function
alexandru-pocovnicu Oct 30, 2025
ee2fd08
Format dedupe tests for consistency and readability
alexandru-pocovnicu Oct 30, 2025
584bd6f
Fix spacing in comment for consistency in includes tests
alexandru-pocovnicu Oct 30, 2025
908c81e
Refactor includes function to use for...of loop for improved readability
alexandru-pocovnicu Oct 30, 2025
05a2a70
updated the median variable to be more compact
alexandru-pocovnicu Nov 23, 2025
29f30d2
removed unnecessary copy of array
alexandru-pocovnicu Nov 23, 2025
235fbc6
add test to check that dedupe returns a copy and not the original array
alexandru-pocovnicu Nov 23, 2025
9fefde9
Refactor sum function to improve NaN handling and streamline number f…
alexandru-pocovnicu Nov 23, 2025
c230c17
changed test for decimal numbers from toEqual to toBeCloseTo
alexandru-pocovnicu Nov 23, 2025
843f101
deleted unnecessary code
alexandru-pocovnicu Nov 23, 2025
49a93ca
updated test to check if dedupe returns a copy of the original array
alexandru-pocovnicu Nov 23, 2025
3d8bc26
filtered out NaN from the elements
alexandru-pocovnicu Nov 23, 2025
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
20 changes: 20 additions & 0 deletions Module-Data-Groups.code-workspace
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"folders": [
{
"name": "Sprint-1",
"path": "./Sprint-1"
},
{
"name": "Sprint-2",
"path": "./Sprint-2"
},
{
"name": "Sprint-3",
"path": "./Sprint-3"
}
],
"settings": {
"jest.disabledWorkspaceFolders": ["Sprint-2", "Sprint-3"],
"jest.jestCommandLine": "npm test --"
}
}
22 changes: 20 additions & 2 deletions Sprint-1/fix/median.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,26 @@
// 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;
}
if (list.length === 0) {
return null;
}
const filteredList = list.filter((element) => typeof element === "number");
const sortedList = filteredList.sort((a, b) => a - b);

const middleIndex = Math.floor(sortedList.length / 2);

const median = sortedList[middleIndex];

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

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

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

return [...new Set(arr)];
}
module.exports = dedupe;
13 changes: 12 additions & 1 deletion Sprint-1/implement/dedupe.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,23 @@ 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", () => {
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("Given an array with no duplicates,return a copy of the original array", () => {
const input = [1, "y", 4, 7];
const result = dedupe(input);
expect(result).toEqual(input);
expect(result).not.toBe(input);
});

// 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,return an array without duplicates", () => {
expect(dedupe([1, 1, "l", 5, 7, "l"])).toEqual([1, "l", 5, 7]);
});
13 changes: 13 additions & 0 deletions Sprint-1/implement/max.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
function findMax(elements) {
if (elements.length === 0) {
return -Infinity;
} else if (elements.length === 1) {
return elements[0];
}
if (elements.every((element) => typeof element !== "number")) {
return NaN;
}
let filteredElements = elements.filter(
(element) => typeof element === "number"
);
let orderedElements = filteredElements.sort((a, b) => a - b);
return orderedElements[orderedElements.length - 1];
}

module.exports = findMax;
22 changes: 21 additions & 1 deletion Sprint-1/implement/max.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,48 @@ 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([])).toEqual(-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([4])).toEqual(4);
});

// 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,return the largest number overall", () => {
expect(findMax([-9, 4])).toEqual(4);
});

// 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 zero", () => {
expect(findMax([-3, -1, -9, -6])).toEqual(-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,return the largest decimal number", () => {
expect(findMax([4.7, 4.1, 9.5, 1.3])).toEqual(9.5);
});

// 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,return the max and ignore non-numeric values", () => {
expect(findMax(["99", 4, null, "h", 2, 3.9])).toEqual(4);
});

// 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,return isNaN", () => {
expect(findMax([null, undefined, "8", "g"])).toBeNaN();
});
17 changes: 16 additions & 1 deletion Sprint-1/implement/sum.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
function sum(elements) {
}
if (elements.length === 0) {
return 0;
}

const filteredElements = elements.filter(
(element) => typeof element === "number" && !Number.isNaN(element)
);
if (filteredElements.length === 0) {
return NaN;
}
let addElements = 0;
for (let element of filteredElements) {
addElements = addElements + element;
}

return addElements;
}
module.exports = sum;
20 changes: 19 additions & 1 deletion Sprint-1/implement/sum.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,42 @@ 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([])).toEqual(0);
});

// 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 that number", () => {
expect(sum([5])).toEqual(5);
});

// 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,return the correct total sum", () => {
expect(sum([-6, -7, -2])).toEqual(-15);
});

// 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,return the correct total sum", () => {
expect(sum([-1.5, 1.5, 4.5])).toBeCloseTo(4.5);
expect(sum([-1.5, 1.5, 4.5])).toBeCloseTo(4.5,5);
});

// 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 ignore the non-numerical values and return the sum of the numerical elements", () => {
expect(sum([null, "g", "9", 1.2, 1.8, -3])).toEqual(0);
});

// 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,return Nan", () => {
expect(sum(["g", undefined, null, "7", []])).toBeNaN();
});
Loading