Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 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
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.slice(middleIndex)[0];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • There is a more compact and more efficient way to access the element at position middleIndex.


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

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

Expand Down
12 changes: 11 additions & 1 deletion Sprint-1/implement/dedupe.js
Original file line number Diff line number Diff line change
@@ -1 +1,11 @@
function dedupe() {}
function dedupe(arr) {
if (arr.length === 0) {
return [];
}
const arrCopy = arr.slice(0);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line 7 will create a new array. We don't have to create another array here.

console.log(arrCopy);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you need this console.log() statement for the function to work?

return [...new Set(arrCopy)];
}
module.exports = dedupe;

console.log(dedupe([1, 2, 3, 4, 4, 2]));
10 changes: 9 additions & 1 deletion Sprint-1/implement/dedupe.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,20 @@ 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", () => {
expect(dedupe([1, "y", 4, 7])).toEqual([1, "y", 4, 7]);
});
Comment on lines +26 to +28
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current test code cannot check if the returned array is a copy of the original array because toEqual() compares objects (including arrays) by value. To illustrate,

  const A = [2, 3, 1];
  const B = [...A];          // B is a copy of A
    
  // This set of code cannot distinguish if the compared objects are the same objects.
  expect(A).toEqual(A);  // true
  expect(A).toEqual(B);  // true

In order to check if the returned array is a copy of the original array, we would need additional checks.
Can you find out what code you need to add in order to ensure the returned value is not the original array?


// 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];
Comment on lines +2 to +14
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • You could also consider treating an array that doesn’t contain any numbers as an empty array; both of them do not contain any numbers.

  • Does your function return the value you expect from the following function calls?

  findMax([ "Foo" ]);
  findMax([ "Foo", "Bar" ]);
  findMax([ NaN, 1, 2, 3, NaN ]);

}

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();
});
20 changes: 19 additions & 1 deletion Sprint-1/implement/sum.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,22 @@
function sum(elements) {
}
if (elements.length === 0) {
return 0;
}
if (elements.length === 1) {
return elements[0];
}
Comment on lines +2 to +7
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why give special treatment to an array with one element?

if (elements.every((element) => typeof element !== "number")) {
return NaN;
}
const filteredElements = elements.filter(
(element) => typeof element === "number"
);
Comment on lines +8 to +13
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When an array contains at least one number, the code on lines 8-13 would end up processing the array elements twice.
Can you figure out a more efficient approach that only requires processing the array elements once?

let addElements = 0;
for (let element of filteredElements) {
addElements = addElements + element;
}

return addElements;
}
module.exports = sum;
console.log(sum([-9, -3, -4]));
19 changes: 18 additions & 1 deletion Sprint-1/implement/sum.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,41 @@ 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])).toEqual(4.5);
});
Comment on lines +37 to +39
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Decimal numbers in most programming languages (including JS) are internally represented in "floating point number" format. Floating point arithmetic is not exact. For example, the result of 46.5678 - 46 === 0.5678 is false because 46.5678 - 46 only yield a value that is very close to 0.5678. Even changing the order in which the program add/subtract numbers can yield different values.

So the following could happen

  expect( 1.2 + 0.6 + 0.005 ).toEqual( 1.805 );                // This fail
  expect( 1.2 + 0.6 + 0.005 ).toEqual( 1.8049999999999997 );   // This pass
  expect( 0.005 + 0.6 + 1.2 ).toEqual( 1.8049999999999997 );   // This fail

  console.log(1.2 + 0.6 + 0.005 == 1.805);  // false
  console.log(1.2 + 0.6 + 0.005 == 0.005 + 0.6 + 1.2); // false

Can you find a more appropriate way to test a value (that involves decimal number calculations) for equality?

Suggestion: Look up

  • Checking equality in floating point arithmetic in JavaScript
  • Checking equality in floating point arithmetic with Jest


// 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
Loading