Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
cae4a14
rewrite function
vmoratti Jul 24, 2026
f9211a3
write function and create two tests
vmoratti Jul 25, 2026
183be23
change function, write some tests
vmoratti Jul 25, 2026
8e59e19
write test for decimal numbers
vmoratti Jul 25, 2026
f9080a3
write test for mixed array of numbers and non-numbers
vmoratti Jul 25, 2026
72267a0
write test for mixed array of non-numbers
vmoratti Jul 25, 2026
4f2f012
add empty array test
vmoratti Jul 25, 2026
f48b3b9
write sum function
vmoratti Jul 26, 2026
4fe95d9
add tests for negative and decimal numbers
vmoratti Jul 26, 2026
120a279
write test for mixed array
vmoratti Jul 26, 2026
a02f092
add validation to the function
vmoratti Jul 26, 2026
a22313c
add test with non-number array
vmoratti Jul 26, 2026
dc82a68
add tests for an empty, and array wwith duplicates
vmoratti Jul 26, 2026
4522a37
write function
vmoratti Jul 26, 2026
c22b7f9
add test with duplicates
vmoratti Jul 26, 2026
8f921db
refactor includes function
vmoratti Jul 26, 2026
0bfabe2
Fix median function to allow single element lists
vmoratti Jul 31, 2026
23d816f
Fix indentation in dedupe function
vmoratti Jul 31, 2026
9fc3bd1
Update findMax to filter out NaN values
vmoratti Aug 1, 2026
ad1cb78
Fix median function to handle empty filtered list
vmoratti Aug 1, 2026
71b98b5
add test with 300 and new line
vmoratti Aug 2, 2026
b58b9c2
add test to chack for array identity
vmoratti Aug 2, 2026
6604d84
remove if statement
vmoratti Aug 2, 2026
67e3d31
change varaible name to counter
vmoratti Aug 2, 2026
64b4a38
add additional validation
vmoratti Aug 2, 2026
cba37a9
fix indentation
vmoratti Aug 2, 2026
4312a67
change floating point testing to .toBeCloseTo
vmoratti Aug 2, 2026
0a9896d
change variable name to element
vmoratti Aug 2, 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
32 changes: 29 additions & 3 deletions Sprint-1/fix/median.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,36 @@
// Hint: Please consider scenarios when 'list' doesn't have numbers (the function is expected to return null)
// or 'list' has mixed values (the function is expected to sort only numbers).

// Fix this implementation
// Start by running the tests for this function
// If you're in the Sprint-1 directory, you can run `npm test -- fix` to run the tests in the fix directory

// Hint: Please consider scenarios when 'list' doesn't have numbers (the function is expected to return null)
// or 'list' has mixed values (the function is expected to sort only numbers).
// Fix this implementation
// Start by running the tests for this function
// If you're in the Sprint-1 directory, you can run `npm test -- fix` to run the tests in the fix directory

// Hint: Please consider scenarios when 'list' doesn't have numbers (the function is expected to return null)
// 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) || list.length < 1) {
return null;
}
const filteredList = list.filter((value) => typeof value === "number");
if (filteredList.length < 1) {
return null;
}
const sortedList = filteredList.sort((a, b) => a - b);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Do explore the difference between .toSorted() and .sort().

const evenLength = sortedList.length % 2 === 0;
const middleIndex = Math.floor(sortedList.length / 2);
if (evenLength) {
const evenListCalc =
(sortedList[middleIndex] + sortedList[middleIndex - 1]) / 2;
return evenListCalc;
} else {
return sortedList[middleIndex];
}
}

module.exports = calculateMedian;
13 changes: 12 additions & 1 deletion Sprint-1/implement/dedupe.js
Original file line number Diff line number Diff line change
@@ -1 +1,12 @@
function dedupe() {}
function dedupe(arr) {
let newArr = [];
for (let i = 0; i < arr.length; i++) {
if (!newArr.includes(arr[i])) {
newArr.push(arr[i]);
}
}
return newArr;
}

console.log(dedupe([1,2,2,2,4]))
module.exports = dedupe;
17 changes: 16 additions & 1 deletion Sprint-1/implement/dedupe.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,28 @@ E.g. dedupe([1, 2, 1]) returns [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, it returns a copy of the original array", () => {
expect(dedupe([1, 2, 3])).toEqual([1, 2, 3]);
});
Comment on lines +26 to +28

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Your function implementation is correct. However, this test could be improved to better ensure
that any future changes continue to align with the expected behavior described on line 25:

Then it should return a copy of the original array

This test should fail if the function returns the original array (instead of a copy of the original array).

The current test checks only if both the original array and the returned array contain identical elements.
In order to validate the returned array is a different array, we need an additional check.

Can you find out what this additional check is?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

i have now added test to check if the returned array is a copy


// Given an array of strings or numbers
// When passed to the dedupe function
// Then it should return a new array with duplicates removed while preserving the
// first occurrence of each element from the original array.
test("given an array of strings or numbers, it returns a new array with no duplicates removed", () => {
expect(dedupe(['a','a','a','b','b','c'])).toEqual(['a','b','c']);
expect(dedupe([5, 1, 1, 2, 3, 2, 5, 8])).toEqual([5, 1, 2, 3, 8]);
});

const original = [1, 2, 3];
const result = dedupe(original);

expect(result).toEqual([1, 2, 3]);
expect(result).not.toBe(original);
Comment on lines +39 to +43

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This test is correct, but shouldn't it be defined at line 27?

8 changes: 7 additions & 1 deletion Sprint-1/implement/max.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
function findMax(elements) {

const filteredList = elements.filter(
(value) => typeof value === "number" && !Number.isNaN(value)
);
return Math.max(...filteredList);

}

module.exports = findMax;
module.exports = findMax;
35 changes: 34 additions & 1 deletion Sprint-1/implement/max.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,61 @@ 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
describe("findMax", () => {
[{ input: [3], expected: 3 }].forEach(({ input, expected }) =>
it(`returns the only number in the array for [${input}]`, () =>
expect(findMax(input)).toEqual(expected))
);
});

// 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", () => {
expect(findMax([1, -2, 3, -4, 5])).toEqual(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 closest to zero", () => {
expect(findMax([-1, -2, -3, -4])).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, returns largest decimal number", () => {
expect(findMax([1.5, 1.6, 1.7, 1.8])).toEqual(1.8);
expect(findMax([-1.5, -1.6, -1.7, -1.8])).toEqual(-1.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, returns the max and ignores non-numeric values", () => {
expect(findMax([1, 2, "3", null, undefined, 4])).toEqual(4);
expect(findMax(["apple", 1, 2, 3, "banana", 4])).toEqual(4);
expect(findMax([1, "2", 3, "4", 5])).toEqual(5);
expect(findMax([1, "apple", 2, null, 3, undefined, 4])).toEqual(4);
expect(findMax([3, "apple", 1, null, 2, undefined, 4])).toEqual(4);
expect(findMax(["banana", 5, 3, "apple", 1, 4, 2])).toEqual(5);
expect(findMax([1, 2, 3, "4", 5, "300", 7])).toEqual(7);
});

// 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, returns -Infinity", () => {
expect(findMax(["apple", null, undefined])).toEqual(-Infinity);
expect(findMax([null, undefined])).toEqual(-Infinity);
expect(findMax(["apple", "banana"])).toEqual(-Infinity);
});

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) {
let counter = 0;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Could also use total.

const filteredList = elements.filter((value) => typeof value === "number" && !isNaN(value) && isFinite(value));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Note: Number.isFinite() checks all three conditions.

for (let i = 0; i < filteredList.length; i++) {
counter += filteredList[i];
}
return counter;
}

module.exports = sum;

console.log(sum([NaN, 1]));
console.log(sum([Infinity, -Infinity]));
22 changes: 21 additions & 1 deletion Sprint-1/implement/sum.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,44 @@ 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 returns that number", () => {
expect(sum([2])).toEqual(2);
});

// 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, returns the correct total sum", () => {
expect(sum([-1, -2, -3])).toEqual(-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, returns the correct total sum", () => {
expect(sum([1.5, 2.5, 3.5])).toBeCloseTo(7.5);
expect(sum([-1.5, -2.5, -3.5])).toBeCloseTo(-7.5); // Using toBeCloseTo for floating point precision
});
Comment thread
cjyuan marked this conversation as resolved.

// 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, ignores the non-numerical values and returns the sum of the numerical elements", () => {
expect(sum([1, 2, "3", null, undefined, 4])).toEqual(7);
expect(sum(["apple", 1, 2, 3, "banana", 4])).toEqual(10);
expect(sum([1, "2", 3, "4", 5])).toEqual(9);
});

// 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, returns 0", () => {
expect(sum(["apple", "banana", null, undefined])).toEqual(0);
});
5 changes: 3 additions & 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 All @@ -11,3 +10,5 @@ function includes(list, target) {
}

module.exports = includes;


Loading