Skip to content
Open
Show file tree
Hide file tree
Changes from 5 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
34 changes: 30 additions & 4 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).

function calculateMedian(list) {
const middleIndex = Math.floor(list.length / 2);
const median = list.splice(middleIndex, 1)[0];
return median;
function calculateMedian(list) {
let newList = 0;

// validate input
if (list === null || !Array.isArray(list) || !list[0])
return null;

// filter out non-numeric values inside the list
list = list.filter(item => Number.isInteger(item));
Copy link

Choose a reason for hiding this comment

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

Good stuff. You’re currently using Number.isInteger() to check numbers. Could there be a better way to include floating-point numbers in your median calculation too?

Copy link
Author

Choose a reason for hiding this comment

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

Yes, will be better to use Number.isFinite() instead of Number.isInteger if we want to include floating-point numbers.
I updated filter() so floating-point numbers are included.


// to avoid dealing with empty arrays
if (list.length === 0)
return null;

// sort the list
list.sort((a, b) => a - b);

if ( list.length % 2 == 0 ) { // even
// return the sum of the two middle numbers divided by 2
const middleIndex = list.length / 2;
newList = list[middleIndex - 1] + list[middleIndex];
newList = newList / 2;
return newList;
} // else will be odd

// just return the middle number
newList = list[Math.floor(list.length / 2)];

return newList;
}

console.log(calculateMedian(["apple", null, undefined]));
module.exports = calculateMedian;
8 changes: 7 additions & 1 deletion Sprint-1/implement/dedupe.js
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
function dedupe() {}
function dedupe(arr) {
arr = new Set(arr); // Using `new Set()` to store only unique values in the same variable
Copy link

Choose a reason for hiding this comment

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

Good use use of Set.

Copy link
Author

Choose a reason for hiding this comment

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

Thank you ^^

return Array.from(arr); // return the new array using `Array.from()`
}

console.log(dedupe(['a','V','a','b','b','c'])); // ['a','b','c']
module.exports = dedupe;
20 changes: 18 additions & 2 deletions Sprint-1/implement/dedupe.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,28 @@ 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.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.todo("given an empty with no duplicates, it returns 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]);
expect(dedupe(['a', 'b', 'c'])).toEqual(['a', 'b', 'c']);
expect(dedupe(['H', 'E', 'L', 'O', '2', '0'])).toEqual(['H', 'E', 'L', 'O', '2', '0']);
expect(dedupe([5])).toEqual([5]);
});
// 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
//test.todo("given an array with strings or numbers, it should remove duplicate values, preserving the first occurence of each element");
test("given an array with strings or numbers, it should remove duplicate values, preserving the first occurence of each element", () => {
expect(dedupe(['a', 'V', 'a', 'b', 'b', 'c'])).toEqual(['a', 'V', 'b', 'c']);
expect(dedupe(['x', 'y', 'x', 'z', 'y', 'x'])).toEqual(['x', 'y', 'z']);
expect(dedupe([5, 1, 1, 2, 3, 2, 5, 8])).toEqual([5, 1, 2, 3, 8]);
expect(dedupe([1, 2, 1])).toEqual([1, 2]);
});
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) {
let max = -Infinity; // smallest possible value in JS
for (const element of elements) {
// Check if the element is a finite number and greater than current max
if (Number.isFinite(element) && element > max) {
max = element; // will always be the greatest so far
}
}
return max;
}

//console.log(findMax([1.5, 2.3, 0.7, 3.9, 2.8])); // should return 3.9
module.exports = findMax;
38 changes: 38 additions & 0 deletions Sprint-1/implement/max.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,65 @@ const findMax = require("./max.js");
// 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([])).toBe(-Infinity);
});

// Given an array with one number
// When passed to the max function
// Then it should return that number
test.todo("given an array with one number, returns that number");
test("given an array with one number, returns that number", () => {
expect(findMax([7842])).toBe(7842);
expect(findMax([337])).toBe(337);
expect(findMax([1])).toBe(1);
});

// Given an array with both positive and negative numbers
// When passed to the max function
// Then it should return the largest number overall
test.todo("given an array with both positive and negative numbers, returns the largest number overall");
test("given an array with both positive and negative numbers, returns the largest number overall", () => {
expect(findMax([-10, 0, 5, -3, 8, 2])).toBe(8);
expect(findMax([-20, -5, -1, -15])).toBe(-1);
expect(findMax([3, 7, 2, 9, 4])).toBe(9);
});

// Given an array with just negative numbers
// When passed to the max function
// Then it should return the closest one to zero
test.todo("given an array with just negative numbers, returns the closest one to zero");
test("given an array with just negative numbers, returns the closest one to zero", () => {
expect(findMax([-8, -3, -15, -1, -7])).toBe(-1);
expect(findMax([-50, -20, -30, -10])).toBe(-10);
});

// Given an array with decimal numbers
// When passed to the max function
// Then it should return the largest decimal number
test.todo("given an array with decimal numbers, returns the largest decimal number");
test("given an array with decimal numbers, returns the largest decimal number", () => {
expect(findMax([1.5, 2.3, 0.7, 3.9, 2.8])).toBe(3.9);
expect(findMax([-1.2, -0.5, -3.4, -0.1])).toBe(-0.1);
expect(findMax([0.1, 0.01, 0.001, 0.0001])).toBe(0.1);
});

// 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.todo("given an array with non-number values, returns the max and ignores non-numeric values");
test("given an array with non-number values, returns the max and ignores non-numeric values", () => {
expect(findMax([10, 'hello', 25, null, 15])).toBe(25);
expect(findMax(['world', -5, -10, undefined, -2])).toBe(-2);
expect(findMax([3.5, 'test', 4.2, {}, 2.8])).toBe(4.2);
});

// 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.todo("given an array with only non-number values, returns -Infinity");
test("given an array with only non-number values, returns -Infinity", () => {
expect(findMax(['a', 'b', 'c'])).toBe(-Infinity);
expect(findMax([null, undefined, {}])).toBe(-Infinity);
expect(findMax([true, false, []])).toBe(-Infinity);
});
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) {
let sum = 0;

for ( const element of elements) {
if (Number.isFinite(element)) {
sum += element;
}
}

return sum;
}

console.log(sum([0.5, 2.5, 3.5]));
module.exports = sum;
36 changes: 34 additions & 2 deletions Sprint-1/implement/sum.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,56 @@ 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.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.todo("Given an array with just one number, return that number");
test( "Given an array with just one number, return that number", () => {
expect(sum([4] )).toEqual(4 );
expect(sum([99])).toEqual(99);
expect(sum([81])).toEqual(81);
});

// Given an array containing negative numbers
// When passed to the sum function
// Then it should still return the correct total sum
test.todo("Given an array containing negative numbers, return the correct total sum");
test( "Given an array containing negative numbers, return the correct total sum", () => {
expect(sum([-81, -4, 44, -12])).toEqual(-53);
expect(sum([-10, 20, -30, 40])).toEqual(20);
expect(sum([-5, -15, -10])).toEqual(-30);
});

// Given an array with decimal/float numbers
// When passed to the sum function
// Then it should return the correct total sum
test.todo("Given an array with decimal/float numbers, return the correct total sum");
test( "Given an array with decimal/float numbers, return the correct total sum", () => {
expect(sum([0.5, 2.5, 3.5])).toEqual(6.5);
expect(sum([1.1, 2.2, 3.3])).toEqual(6.6);
expect(sum([10.5, 20.25, 30.75])).toEqual(61.5);
});

// 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.todo("Given an array containing non-number values, ignore them and return the sum of the numerical elements");
test( "Given an array containing non-number values, ignore them and return the sum of the numerical elements", () => {
expect(sum(['hey', 10, 'hi', 60, 10])).toEqual(80);
expect(sum([null, 5, undefined, 15, 'test'])).toEqual(20);
expect(sum([true, 20, false, 30, 'hello'])).toEqual(50);
});
// 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.todo("Given an array with only non-number values, returns 0");
test( "Given an array with only non-number values, returns 0", () => {
expect(sum(['a', 'b', 'c'])).toEqual(0);
expect(sum([null, undefined, 'test'])).toEqual(0);
expect(sum([true, false, 'hello'])).toEqual(0);
});
12 changes: 4 additions & 8 deletions Sprint-1/refactor/includes.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
// 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];
if (element === target) {
return true;
}
}
return false;
const includes = (list, target) =>{
Copy link

Choose a reason for hiding this comment

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

While this may work in most JS environments, could you double check the for...of syntax and see if ther is a better way to write this.

Copy link
Author

Choose a reason for hiding this comment

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

I got it. I didn't declare the variable 'item' properly. I should have put 'let' or 'const' before the variable. It works, but the code fails when running in strict mode ( "use strict"; ). It is safer to declare it the right way. I updated the code so the for..of syntax is better and safer.

for (item of list)
if ( item == target ) return true;
return false;
}

module.exports = includes;