Skip to content
49 changes: 45 additions & 4 deletions Sprint-1/fix/median.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,51 @@
// 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;
function calculateMedian(list) {
// [ 'not an array', 123, null, undefined, {}, [], ["apple", null, undefined] ]
if (typeof list === 'string') {
return null
}
if (typeof list === 'number') {
return null
}

if (list === null) {
return null
}
if (typeof list === 'undefined') {
return null
}

if (list === undefined) {
return null
}
if (typeof list === 'object' && !Array.isArray(list)) {
return null
}
Comment on lines +10 to +29

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

These cases are all return null and can be simplified into one.

if (Array.isArray(list) && list.length === 0){
return null
}
list = list.filter(element => typeof element === 'number');
if (list.length === 0) {
return null
}
list.sort((a, b) => a - b);

if (list.length % 2 === 0){
const middleIndexR = Math.floor(list.length / 2);
const middleIndexL = middleIndexR - 1
const evenMedian = (list[middleIndexL] + list[middleIndexR]) / 2;

return evenMedian
} else {
const middleIndex = Math.floor(list.length / 2);
const median = list[middleIndex];
return median;
}

}

calculateMedian(['apple'])

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

please move any test case into median.test.js, thank you.


module.exports = calculateMedian;
6 changes: 3 additions & 3 deletions Sprint-1/fix/median.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ describe("calculateMedian", () => {
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("doesn't modify the input array [3, 1, 2]", () => {
const list = [3, 1, 2];
calculateMedian(list);
expect(list).toEqual([3, 1, 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 },
Expand All @@ -47,4 +47,4 @@ describe("calculateMedian", () => {
].forEach(({ input, expected }) =>
it(`filters out non-numeric values and calculates the median for [${input}]`, () => expect(calculateMedian(input)).toEqual(expected))
);
});
});
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(list) {
return [...new Set(list)];
}

module.exports = dedupe;
11 changes: 10 additions & 1 deletion Sprint-1/implement/dedupe.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,22 @@ 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 an a copy", () => {
expect(dedupe([1, 2, 3,])).toEqual([1, 2, 3,]);
});


// 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 with duplicate strings and numbers, it returns the original with duplicates removed", () => {
expect(dedupe([1, 'apple', 'horse', 'horse', 2, 2, 3, 'bat'])).toEqual([1, 'apple', 'horse', 2, 3, 'bat']);
});
6 changes: 5 additions & 1 deletion Sprint-1/implement/max.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
function findMax(elements) {
elements = elements.filter(element => typeof element === 'number');
if (elements.length === 0){
return -Infinity;
}
return Math.max(...elements);
}

module.exports = findMax;
23 changes: 22 additions & 1 deletion Sprint-1/implement/max.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,49 @@ 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 the same 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, returns the largest number", () => {
expect(findMax([10, -10])).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("given an array with negative numbers, returns the number closest to zero", () => {
expect(findMax([-3 , -15, -4, -11])).toEqual(-3);
});


// 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 negative numbers, returns the number closest to zero", () => {
expect(findMax([-3 , -15, -4, -11])).toEqual(-3);
});
Comment on lines 45 to +50

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This test case is the same as above. It should be testing decimal numbers (e.g. all positive, all negative and combined).


// 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, ignore the non numeric values and return the max", () => {
expect(findMax(['car', 'house', 1, 12, 'fish'])).toEqual(12);
});

// 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(['car', 'house', 'fish', 'car'])).toEqual(-Infinity);
});
13 changes: 13 additions & 0 deletions Sprint-1/implement/sum.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
function sum(elements) {
elements = elements.filter(element => typeof element === 'number');
if (elements.length === 0) {
return null;}
Comment on lines +3 to +4

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

According to the test case, given an empty array, it should return 0 but not null.


let total = 0;

for (let element of elements) {
total += element;
// += means to add to the current value and assign as result
// elements are individual items inside a collection (not just string)
}

return total;
}

module.exports = sum;
20 changes: 19 additions & 1 deletion Sprint-1/implement/sum.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,42 @@ 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 the same 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 containing negative numbers, returns the total sum", () => {
expect(sum([-4, -15, -12])).toEqual(-31);
});

// 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 sum", () => {
expect(sum([4.1, 5.4, 7.8])).toEqual(17.3);
});

// 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 non-number values, ignores the non-number values and returns the correct sum", () => {
expect(sum(['fish', 4, 'dog', 5, ,'pizza', 10])).toEqual(19);
});

// 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 of only non-numeric values, returns 0", () => {
expect(sum(['fish','dog','pizza'])).toEqual(0);
});
4 changes: 2 additions & 2 deletions Sprint-1/refactor/includes.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// 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;
}
}
return false;
}


module.exports = includes;
Loading