Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
25 changes: 22 additions & 3 deletions Sprint-1/fix/median.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,28 @@
// 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;
// Step 1: must be an array
if (!Array.isArray(list)) return null;

// Step 2: filter numeric values
const numericValues = list.filter(
(item) => typeof item === "number" && !Number.isNaN(item)
);

// Step 3: must have at least one number
if (numericValues.length === 0) return null;

// Step 4: sort safely
const sortedList = numericValues.slice().sort((a, b) => a - b);
Copy link
Contributor

Choose a reason for hiding this comment

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

Why make a copy of numericValues before sorting?


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

// Step 5: median
if (sortedList.length % 2 !== 0) {
return sortedList[middleIndex];
}

return (sortedList[middleIndex - 1] + sortedList[middleIndex]) / 2;
}

module.exports = calculateMedian;
6 changes: 5 additions & 1 deletion Sprint-1/implement/dedupe.js
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
function dedupe() {}
function dedupe(elements) {
if (elements.length === 0) return [];
return elements.filter((item, index) => elements.indexOf(item) === index);
}
module.exports = dedupe;
23 changes: 21 additions & 2 deletions Sprint-1/implement/dedupe.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,31 @@ 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 input = [];
const result = dedupe(input);
expect(result).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 return a copy of the original array", () => {
const input = [1, 2, 3, 4, 5];
const result = dedupe(input);
expect(result).toEqual([1, 2, 3, 4, 5]);
});
Comment on lines 25 to +32
Copy link
Contributor

Choose a reason for hiding this comment

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

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?


// 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
// Then it should remove the duplicate values, preserving the first occurrence of each element
test("given an array with strings, it removes the duplicate, preserving first occurrence of each element", () => {
const input = ["a", "a", "a", "b", "b", "c"];
const result = dedupe(input);
expect(result).toEqual(["a", "b", "c"]);
});
test("given an array with numbers it removes the duplicate, preserving first occurrence of each element", () => {
const input = [5, 1, 1, 2, 3, 2, 5, 8];
const result = dedupe(input);
expect(result).toEqual([5, 1, 2, 3, 8]);
});
6 changes: 6 additions & 0 deletions Sprint-1/implement/max.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
function findMax(elements) {
if (!Array.isArray(elements) || elements.length === 0) return -Infinity;
return Math.max(
...elements.filter(
(item) => typeof item === "number" && !Number.isNaN(item)
)
);
}

module.exports = findMax;
36 changes: 35 additions & 1 deletion Sprint-1/implement/max.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,62 @@ 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 input = []
const result = findMax(input)
expect(result).toBe(-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, its should return that number', () => {
const input = [5]
const result = findMax(input)
expect(result).toBe(5);
});

// 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, it should return largest overall', () => {
const input = [5, -5, -10, -1, 6, 8, 10]
const result = findMax(input)
expect(result).toBe(10);
});

// 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, it should return the closest one to zero', () => {
const input = [-10, -5, -2, -8];
const result = findMax(input);
expect(result).toBe(-2);
})

// 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 just decimal numbers, it should return the largest decimal number", () => {
const input = [1.0, 2.5, 2.2, 5.8];
const result = findMax(input);
expect(result).toBe(5.8);
});

// 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 value, it should return the max and ignore non-numeric value", () => {
const input = [4, 5, 'arr', 2, []," ", 15, 8];
const result = findMax(input);
expect(result).toBe(15);
});

// 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 value, it should return the least surprising value given how it behaves for all other inputs", () => {
const input = [true, {}, "arr", null, [], " ", NaN];
const result = findMax(input);
expect(result).toBe(-Infinity);
});
Comment on lines 61 to +77
Copy link
Contributor

Choose a reason for hiding this comment

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

When a string representing a valid numeric literal (for example, "300") is compared to a number,
JavaScript first converts the string into its numeric equivalent before performing the comparison.
As a result, the expression 20 < "300" evaluates to true.

To test if the function can correctly ignore non-numeric values,
consider including a string such as "300" in the relevant test cases.

4 changes: 4 additions & 0 deletions Sprint-1/implement/sum.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
function sum(elements) {
if (!Array.isArray(elements) || elements.length === 0) return 0;
return elements
.filter((item) => typeof item === "number" && !Number.isNaN(item))
.reduce((acc, n) => acc + n, 0);
}

module.exports = sum;
31 changes: 30 additions & 1 deletion Sprint-1/implement/sum.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,53 @@ 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 input = [];
const result = sum(input);
expect(result).toBe(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, it should return that number", () => {
const input = [5];
const result = sum(input);
expect(result).toBe(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, it should return the correct total sum", () => {
const input = [5, -6, 10, -12];
const result = sum(input);
expect(result).toBe(-3);
});

// 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, it should return the correct total sum", () => {
const input = [5, 6.6, 10, 5.12];
const result = sum(input);
expect(result).toBe(26.720000000000002);
});
Comment on lines 40 to +47
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 numerical values and return sum of numeric element", () => {
const input = [4, 5, "arr", 2, [], " ", 15, 8];
const result = sum(input);
expect(result).toBe(34);
});

// 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, it should return least surprising value given how it behaves for all other inputs", () => {
const input = [true, {}, "arr", null, [], " ", NaN];
const result = sum(input);
expect(result).toBe(0);
});
10 changes: 8 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 (const element of list) {
if (element === target) {
return true;
}
Expand All @@ -11,3 +10,10 @@ function includes(list, target) {
}

module.exports = includes;

// const array = ["a", "b", "c"];

// for (const element of array) {
// console.log(element);
// } for (variable of iterable) statement

Comment on lines +13 to +19
Copy link
Contributor

Choose a reason for hiding this comment

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

It's best practice to remove unnecessary code to keep the code clean.

Loading