Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
2071acd
fixed median calculation for odd and even arrays
marscancode Nov 3, 2026
19742a7
Sorted a copy before calculating the median
marscancode Nov 3, 2026
aca8e91
Added a check in function to make sure the input is an array
marscancode Nov 3, 2026
091e875
Added a a check in function for empty arrays
marscancode Nov 3, 2026
c4d3648
added check in function that the array only contains numbers
marscancode Nov 3, 2026
fc92cc2
removed duplicate values from arrays
marscancode Nov 3, 2026
a078653
added describe median function
marscancode Nov 3, 2026
e02094b
Added tests for describeMedian
marscancode Nov 3, 2026
1875b30
Ignore prep directory
marscancode Nov 3, 2026
f9a47c7
Implemented findMax function
marscancode Nov 3, 2026
1516690
added empty and single-number tests for findMax
marscancode Nov 3, 2026
445b260
Added test for both positive and negative numbers
marscancode Nov 3, 2026
4337038
Add test for array with negative numbers
marscancode Nov 3, 2026
6fd0acf
added test for decimal numbers
marscancode Nov 3, 2026
8490f09
added test for value that isnt a number
marscancode Nov 3, 2026
7b42dd0
Completed findMax tests
marscancode Nov 3, 2026
9f17e6d
added first test for calculateMean, it fails
marscancode Nov 4, 2026
19d9c71
implemented function calculateMean
marscancode Nov 4, 2026
063182a
test to check wether calculateMean works given an array with a single…
marscancode Nov 4, 2026
1b1468e
test to check how calculateMean handles an array of negative numbers,…
marscancode Nov 4, 2026
d90b2cf
test to check wether calculateMean handles an array of decimal number…
marscancode Nov 4, 2026
7547c25
added test to check wether calculateMean throws an error when given a…
marscancode Nov 4, 2026
c87f412
implemented calculateMean to handle empty arrays
marscancode Nov 4, 2026
f24628a
test to check if calculateMean throws an error when given a string in…
marscancode Nov 4, 2026
c574535
implemented calculateMean to handle a string, tests pass
marscancode Nov 4, 2026
c427aa2
added tests for calculateMean for invalid non-array values
marscancode Nov 4, 2026
108c621
deleted duplicate empty array test
marscancode Nov 4, 2026
03ec939
added test to check if calculateMedian throws an error when given an …
marscancode Nov 4, 2026
3048952
implemented calculate mean to only take in arrays with number values
marscancode Nov 4, 2026
e6bec3a
added test for empty sum array
marscancode Nov 4, 2026
15f68c9
implemented the sum function using reduce, test passes
marscancode Nov 4, 2026
e4a79ce
added test to check if an array with one number returns the number
marscancode Nov 4, 2026
51a5109
added test to check if sum can handle negative numbers
marscancode Nov 4, 2026
597c5f5
added test for sum with decimal numbers
marscancode Nov 4, 2026
28a59d0
added test to check if sum throws an error when given an array contai…
marscancode Nov 4, 2026
4e83c4c
implemented sum function using reduce
marscancode Nov 4, 2026
c9f7754
implemented sum function to throw an error if given an array containi…
marscancode Nov 4, 2026
75d26ab
added test for non-array input in sum function
marscancode Nov 4, 2026
9abe6cc
implemented sum function to check array input if incorrect throw error
marscancode Nov 4, 2026
80d4434
Add tests for non-array and missing sum arguments
marscancode Nov 4, 2026
9ea7f04
added test for number input in sum function
marscancode Nov 4, 2026
556c1a7
refactored includes to use a for...of loop, tests pass
marscancode Nov 5, 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ node_modules
**/.DS_Store
.idea
package-lock.json
/prep/
23 changes: 20 additions & 3 deletions Sprint-1/fix/median.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,26 @@
// or contains values that aren't numbers (the function is expected to throw - see the tests).

function calculateMedian(list) {
const middleIndex = Math.floor(list.length / 2);
const median = list.splice(middleIndex, 1)[0];
return median;
if (!Array.isArray(list)) {
throw new Error("calculateMedian requires an array of numbers");
}

if (list.length === 0) {
throw new Error("calculateMedian requires a non-empty array");
}

if (!list.every((number) => typeof number === "number")) {
throw new Error("calculateMedian requires an array of numbers");
}
const sortedList = [...list];
sortedList.sort((a, b) => a - b);
Comment on lines +20 to +21

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 Array.prototype.toSorted().

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

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

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(numbers) {
//function starts
const uniqueNumbers = [];

for (let i = 0; i < numbers.length; i++) {
// for loop starts
const currentValue = numbers[i];

if (!uniqueNumbers.includes(currentValue)) {
// if statement starts
uniqueNumbers.push(currentValue);
} // if statement ends
} // for loop ends
Comment on lines +6 to +13

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.

When the code is properly indented, we could already easily match the paring {' and }'. Thus there is no need to use comments to indicate the starts and ends of a code block.


return uniqueNumbers;
} //function ends

module.exports = dedupe;
11 changes: 10 additions & 1 deletion Sprint-1/implement/describe-median.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Don't change this function.
function calculateMedian(list) {


if (!Array.isArray(list)) {
throw new Error("calculateMedian requires an array of numbers");
}
Expand All @@ -20,6 +22,13 @@ function calculateMedian(list) {
}

// Implement this function. See describe-median.test.js for the acceptance criteria.
function describeMedian(list) {}
function describeMedian(list) { // opens function
try { // opens try statement
const median = calculateMedian(list);
return `The median is ${median}`;
} catch (error) { // opens catch
return "Could not calculate a median: " + error.message;
}
} //closes function
Comment on lines +25 to +32

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.

These comments are also not needed when the code is properly indented.


module.exports = describeMedian;
15 changes: 14 additions & 1 deletion Sprint-1/implement/describe-median.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,25 @@ const describeMedian = require("./describe-median.js");
// When passed to describeMedian
// Then it should return "The median is " followed by the median
// Delete this test.todo and replace it with a test.
test.todo('given [1, 2, 3], returns "The median is 2"');
test('given [1, 2, 3], returns "The median is 2"', () => {
expect(describeMedian([1, 2, 3])).toEqual("The median is 2");
});

// Given an empty array
// When passed to describeMedian
// Then it should return "Could not calculate a median: calculateMedian requires a non-empty array"
test("given an empty array returns an error message", () => {
expect(describeMedian([])).toEqual(
"Could not calculate a median: calculateMedian requires a non-empty array"
);
});

// Given something that isn't an array of numbers, e.g. "banana"
// When passed to describeMedian
// Then it should return "Could not calculate a median: calculateMedian requires an array of numbers"

test("given a string returns an error message", () => {
expect(describeMedian("banana")).toEqual(
"Could not calculate a median: calculateMedian requires an array of numbers"
);
});
16 changes: 16 additions & 0 deletions Sprint-1/implement/max.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,20 @@
function findMax(elements) {
if (
!Array.isArray(elements) ||
!elements.every((element) => typeof element === "number")
) {
throw new Error("findMax requires an array of numbers");
}

let max = -Infinity;

for (let i = 0; i < elements.length; i++) {
if (elements[i] > max) {
max = elements[i];
}
}

return max;
}

module.exports = findMax;
38 changes: 33 additions & 5 deletions Sprint-1/implement/max.test.js

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.

How should the function handle an array containing NaN?

Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,56 @@ 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, should return -Infinity", () => {
expect(findMax([])).toEqual(-Infinity);
});

// Given an array with one number
// When passed to the max function
// Then it should return that number
test("expects findMax with an array containing 7 to return 7", () => {
expect(findMax([7])).toEqual(7);
});

// Given an array with both positive and negative numbers
// When passed to the max function
// Then it should return the largest number overall

test("returns largest number given an array with both positive and negative numbers", () => {
expect(findMax([-5, -6, 10, 4])).toEqual(10);
});
// Given an array with just negative numbers
// When passed to the max function
// Then it should return the closest one to zero

test("returns largest number when given an array with negative numbers", () => {
expect(findMax([-4, -7, -9, -14])).toEqual(-4);
});
// Given an array with decimal numbers
// When passed to the max function
// Then it should return the largest decimal number

test("returns largest number when given an array of decimal numbers", () => {
expect(findMax([2.2, 4.3, 6.8, 9.4])).toEqual(9.4);
});
// Given an array containing a value that isn't a number
// When passed to the max function
// Then it should throw Error("findMax requires an array of numbers")

test("given an array containing a value that isn't a number", () => {
expect(() => findMax([4, "hello", 5])).toThrow(
"findMax requires an array of numbers"
);
});
// Given something that isn't an array at all, such as "hey", 42 or no argument
// When passed to the max function
// Then it should throw Error("findMax requires an array of numbers")
test("given something that isn't an array at all", () => {
expect(() => findMax("hey")).toThrow(
"findMax requires an array of numbers"
);

expect(() => findMax(42)).toThrow(
"findMax requires an array of numbers"
);

expect(() => findMax()).toThrow(
"findMax requires an array of numbers"
);
});
17 changes: 16 additions & 1 deletion Sprint-1/implement/mean.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
function calculateMean(list) {}
function calculateMean(list) {
if (!Array.isArray(list)) {
throw new Error("calculateMean requires an array of numbers");
}
if (list.length === 0) {
throw new Error("calculateMean requires a non-empty array");
}
if (!list.every((number) => typeof number === "number")) {
throw new Error("calculateMean requires an array of numbers");
}
const sum = list.reduce((sum, number) => {
return sum + number;
}, 0);
Comment on lines +11 to +13

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.

There are two different sum in this few lines of code. Do you know which of them is the sum on line 12 refers to?


return sum / list.length;
}

module.exports = calculateMean;
47 changes: 41 additions & 6 deletions Sprint-1/implement/mean.test.js

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.

What should calculateMean([Infinity, 0, -Infinity]) or calculateMean([0, NaN]) return?
The same also applies to sum().

Original file line number Diff line number Diff line change
Expand Up @@ -23,28 +23,63 @@ const calculateMean = require("./mean.js");
// When passed to calculateMean
// Then it should return their mean
// Delete this test.todo and replace it with a test.
test.todo("given [1, 2, 6], returns 3");
test("given the array [1,2,6], should return the number 3", () => {
expect(calculateMean([1, 2, 6])).toEqual(3);
});

// Given an array with a single number
// When passed to calculateMean
// Then it should return that number
test("given an array with a single number, should return 7", () => {
expect(calculateMean([7])).toEqual(7);
});

// Given an array containing negative or decimal numbers
// When passed to calculateMean
// Then it should return the correct mean

test("given an array of negative numbers, should return -4", () => {
expect(calculateMean([-2, -4, -6])).toEqual(-4);
});
test("given an array of decimal numbers, should return 2.5", () => {
expect(calculateMean([1.5, 2.5, 3.5])).toEqual(2.5);
});
Comment on lines +43 to +45

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.

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 empty array
// When passed to calculateMean
// Then it should throw Error("calculateMean requires a non-empty array")

test("given an empty array, should throw an error", () => {
expect(() => calculateMean([])).toThrow(
"calculateMean requires a non-empty array"
);
});
// Given a value that isn't an array, e.g. "banana", 42, null or {}
// When passed to calculateMean
// Then it should throw Error("calculateMean requires an array of numbers")
test("given a value that isn't an array, should throw an error", () => {
expect(() => calculateMean("banana")).toThrow(
"calculateMean requires an array of numbers"
);

// Given no argument at all
// When passed to calculateMean
// Then it should throw Error("calculateMean requires an array of numbers")
expect(() => calculateMean(42)).toThrow(
"calculateMean requires an array of numbers"
);

expect(() => calculateMean(null)).toThrow(
"calculateMean requires an array of numbers"
);

expect(() => calculateMean({})).toThrow(
"calculateMean requires an array of numbers"
);

expect(() => calculateMean()).toThrow(
"calculateMean requires an array of numbers"
);
});
// Given an array containing a non-number value, e.g. [1, "2", 3]
// When passed to calculateMean
// Then it should throw Error("calculateMean requires an array of numbers")
test("given an array containing a non-number value, should throw an error", () => {
expect(() => calculateMean([1, "2", 3])).toThrow(
"calculateMean requires an array of numbers"
);
});
11 changes: 10 additions & 1 deletion Sprint-1/implement/sum.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
function sum(elements) {
if (!Array.isArray(elements)) {
throw new Error("sum requires an array of numbers");
}
if (!elements.every((element) => typeof element === "number")) {
throw new Error("sum requires an array of numbers");
}
const sum = elements.reduce((sum, number) => {
return number + sum;
}, 0);
Comment on lines +7 to +10

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.

Indentation is off.

return sum;
}

module.exports = sum;
31 changes: 28 additions & 3 deletions Sprint-1/implement/sum.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,49 @@ 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 one number, returns that number", () => {
expect(sum([7])).toEqual(7);
});

// 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 with negative numbers, returns the correct sum", () => {
expect(sum([-4, -6, -2])).toEqual(-12);
});

// 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 numbers, returns the correct sum", () => {
expect(sum([2.5, 3.5, 5.5])).toEqual(11.5);
});

// Given an array containing a value that isn't a number
// When passed to the sum function
// Then it should throw Error("sum requires an array of numbers")

test("given an array containing a non-number, throws an error", () => {
expect(() => sum([10, "hello", 2])).toThrow(
"sum requires an array of numbers"
);
});
// Given something that isn't an array at all, such as "hey", 42 or no argument
// When passed to the sum function
// Then it should throw Error("sum requires an array of numbers")

test("given a non-array input, throws an error", () => {
expect(() => sum("hello")).toThrow("sum requires an array of numbers");
});

test("given no argument, throws an error", () => {
expect(() => sum()).toThrow("sum requires an array of numbers");
});
test("given a number instead of an array, throws an error", () => {
expect(() => sum(42)).toThrow("sum requires an array of numbers");
});
4 changes: 1 addition & 3 deletions Sprint-1/refactor/includes.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// 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 (const element of list) {
if (element === target) {
return true;
}
Expand Down
Loading