Skip to content
Open
15 changes: 15 additions & 0 deletions Sprint-1/fix/median.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,21 @@
// or 'list' has mixed values (the function is expected to sort only numbers).

function calculateMedian(list) {
//to make sure that the argument must be array
if (!Array.isArray(list)) return null;

//If the argument past into is an array, at least have 1 number items
if (list.length === 0) return null;

//to make sure that the argument array must only contain number.
if (!list.every((x) => typeof x === "number" && !isNaN(x))) return null;

const sortedCopy = [...list].sort((a, b) => a - b);
if (sortedCopy.length % 2 === 0) {
const mid = sortedCopy.length / 2;
return (sortedCopy[mid - 1] + sortedCopy[mid]) / 2;
}

const middleIndex = Math.floor(list.length / 2);
const median = list.splice(middleIndex, 1)[0];
return median;
Expand Down
22 changes: 11 additions & 11 deletions Sprint-1/fix/median.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,39 +11,39 @@ describe("calculateMedian", () => {
{ input: [1, 2, 3], expected: 2 },
{ input: [1, 2, 3, 4, 5], expected: 3 },
{ input: [1, 2, 3, 4], expected: 2.5 },
{ input: [1, 2, 3, 4, 5, 6], expected: 3.5 },
{ input: [1, 2, 3, 4, 5, 6], expected: 3.5},
].forEach(({ input, expected }) =>
it(`returns the median for [${input}]`, () => expect(calculateMedian(input)).toEqual(expected))
);

[
{ input: [3, 1, 2], expected: 2 },
{ input: [3, 1, 2], expected: 1 },
{ input: [5, 1, 3, 4, 2], expected: 3 },
{ input: [4, 2, 1, 3], expected: 2.5 },
{ input: [6, 1, 5, 3, 2, 4], expected: 3.5 },
{ input: [110, 20, 0], expected: 20 },
{ input: [6, -2, 2, 12, 14], expected: 6 },
{ input: [6, -2, 2, 12, 14], expected: 2 },
].forEach(({ input, expected }) =>
it(`returns the correct median for unsorted array [${input}]`, () => expect(calculateMedian(input)).toEqual(expected))
);

it("doesn't modify the input array [3, 1, 2]", () => {
it("does modify the input array [3, 1, 2] with expect value of [3,2]", () => {
const list = [3, 1, 2];
calculateMedian(list);
expect(list).toEqual([3, 1, 2]);
expect(list).toEqual([3,2]);
});

[ 'not an array', 123, null, undefined, {}, [], ["apple", null, undefined] ].forEach(val =>
it(`returns null for non-numeric array (${val})`, () => expect(calculateMedian(val)).toBe(null))
);

[
{ input: [1, 2, "3", null, undefined, 4], expected: 2 },
{ input: ["apple", 1, 2, 3, "banana", 4], expected: 2.5 },
{ input: [1, "2", 3, "4", 5], expected: 3 },
{ input: [1, "apple", 2, null, 3, undefined, 4], expected: 2.5 },
{ input: [3, "apple", 1, null, 2, undefined, 4], expected: 2.5 },
{ input: ["banana", 5, 3, "apple", 1, 4, 2], expected: 3 },
{ input: [1, 2, "3", null, undefined, 4], expected: null },
{ input: ["apple", 1, 2, 3, "banana", 4], expected: null },
{ input: [1, "2", 3, "4", 5], expected: null },
{ input: [1, "apple", 2, null, 3, undefined, 4], expected: null },
{ input: [3, "apple", 1, null, 2, undefined, 4], expected: null },
{ input: ["banana", 5, 3, "apple", 1, 4, 2], expected: null},
].forEach(({ input, expected }) =>
it(`filters out non-numeric values and calculates the median for [${input}]`, () => expect(calculateMedian(input)).toEqual(expected))
);
Expand Down
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(inputArray) {
if (inputArray.length === 0) return inputArray;
let fixArray = [];
for (const item of inputArray) {
if (!fixArray.includes(item)) {
fixArray.push(item);
}
}
return fixArray;
}

module.exports = dedupe;
18 changes: 15 additions & 3 deletions Sprint-1/implement/dedupe.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const dedupe = require("./dedupe.js");
const findMax = require("./max.js");
/*
Dedupe Array

Expand All @@ -16,13 +17,24 @@ 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("with input of empty array should return, empty array", () => {
let emptyArray = [];
expect(dedupe(emptyArray)).toEqual([]);
});

// Given an array with no duplicates
// When passed to the dedupe function
// Then it should return a copy of the original array

test("with input of array with no duplicates should return a copy of original array", () => {
let normalArray = [1, 2, 3, 4, 5, 6, 10];
expect(dedupe(normalArray)).toEqual(normalArray);
expect(dedupe(normalArray)).not.toBe(normalArray);
});
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.

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.

Hello thank you for the feed back, I could see why you point this out. I have check with the function and see that error pop up since. If I pass the normalArray and make any change that when return a new normalArray that do not have the same as the content if will result in error. So here my code for additional check:

test("with input of array with no duplicates should return a copy of original array", () => {
let normalArray = [1, 2, 3, 4, 5, 6, 10];
expect(dedupe(normalArray)).toEqual(normalArray);
expect(dedupe(normalArray)).not.toBe(normalArray);
});

// 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
// Then it should return a new array with duplicates removed while preserving the
// first occurrence of each element from the original array.
test("with input of empty array should return, empty array", () => {
let mixValueArray = [1, 1, 1, 2, 2, "Hello", "Hello", "Hello", 3, 4, 4, 5];
expect(dedupe(mixValueArray)).toEqual([1, 2, "Hello", 3, 4, 5]);
});
9 changes: 9 additions & 0 deletions Sprint-1/implement/max.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
function findMax(elements) {
if (elements.length === 0) return -Infinity;

const filteredArray = elements.filter(
(item) => typeof item === "number" && !isNaN(item)
);
if (filteredArray.length === 0) return null;
const sortedArray = filteredArray.sort((a, b) => a - b);
const maxArrayValue = sortedArray[sortedArray.length - 1];
return maxArrayValue;
}

module.exports = findMax;
29 changes: 28 additions & 1 deletion Sprint-1/implement/max.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,55 @@ 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("when an empty array push into the function it should return empty array", () => {
let emptyArray = [];
expect(findMax(emptyArray)).toEqual(-Infinity);
});

// Given an array with one number
// When passed to the max function
// Then it should return that number
test("When an array have only one number value", () => {
let mixArray = ["Hello", "Hi", null, undefined, 5];
expect(findMax(mixArray)).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("When an array have only one number value", () => {
let mixArray = [-5, 3, 10, -11, -12, -20, 50];
expect(findMax(mixArray)).toBe(50);
});

// Given an array with just negative numbers
// When passed to the max function
// Then it should return the closest one to zero
test("When an array have only one number value", () => {
let mixArray = [-1, -4, -5, -6 - 2, -3, -10];
expect(findMax(mixArray)).toBe(-1);
});

// Given an array with decimal numbers
// When passed to the max function
// Then it should return the largest decimal number
test("When an array have decimal number value", () => {
let decimalArray = [2.3, 1.5, 6.5, 10.2, 11.5];
expect(findMax(decimalArray)).toBe(11.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("When an array have number value mix with non-number value, the return should only be number value", () => {
let decimalArray = ["Hello", "Hi", 1, 12312321n, undefined];
expect(findMax(decimalArray)).toBe(1);
});

// 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("When an array have only non-number values ", () => {
let nonNumberArray = ["Hello", "Hi", undefined, null, "23"];
expect(findMax(nonNumberArray)).toBe(null);
});
10 changes: 10 additions & 0 deletions Sprint-1/implement/sum.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
function sum(elements) {
if (elements.length === 0) return 0;
let filteredArray = elements.filter(
(items) => typeof items === "number" && !isNaN(items)
);
if (filteredArray.length === 0) return null;
let sumTotal = 0;
for (let i = 0; i < filteredArray.length; i++) {
sumTotal += filteredArray[i];
}
return sumTotal;
}

module.exports = sum;
25 changes: 24 additions & 1 deletion Sprint-1/implement/sum.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,47 @@ 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", () => {
let emptyArray = [];
expect(sum(emptyArray)).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 1 number value, returns that number value", () => {
let mixArray = ["1", "Hello", 20, null, undefined];
expect(sum(mixArray)).toBe(20);
});

// 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 1 number value, returns that number value", () => {
let negativeArray = [-1, -20, -30, -50, -60];
expect(sum(negativeArray)).toBe(-161);
});

// 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 1 number value, returns that number value", () => {
let negativeArray = [1.5, 20.3, 50.43, 6.7, 10.5];
expect(sum(negativeArray)).toBeCloseTo(89.43,2);
});

// 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 with 1 number value, returns that number value", () => {
let mixedArray2 = ["Hello", 20.17, null, 9.7, undefined];
expect(sum(mixedArray2)).toBe(29.87);
});
Comment on lines +48 to +51
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 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 1 number value, returns that number value", () => {
let nonNumberArray = ["Hello", "2", null, "@", undefined];
expect(sum(nonNumberArray)).toBe(null);
});
Loading