Skip to content
Closed
24 changes: 21 additions & 3 deletions Sprint-1/fix/median.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,27 @@
// 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;

// first of all, checking if 'list' isn't an array
if (!Array.isArray(list)) return null;

const numbers = list.filter(Number.isFinite);

if (numbers.length === 0) return null;
if (numbers.length === 1) return numbers[0];


// using 'spread operator' copies the array without mutating it
numbers.sort((a, b) => a - b);

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

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

}

module.exports = calculateMedian;
16 changes: 15 additions & 1 deletion Sprint-1/implement/dedupe.js
Original file line number Diff line number Diff line change
@@ -1 +1,15 @@
function dedupe() {}
function dedupe(array) {
// let arr = array.filter((item, index) => {

// // indexOf() returns the first index where that value appears in the array.
// if (array.indexOf(item) === index) {
// return item;
// }
// })

// return arr;

return [...new Set(array)];
}

module.exports = dedupe;
17 changes: 16 additions & 1 deletion Sprint-1/implement/dedupe.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,27 @@ 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("return an empty array for 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("if no duplicates, return the copy of the original array", () => {
const input = [1, 3, 5, 7];
const result = dedupe(input);

expect(result).toEqual([1, 3, 5, 7]);
expect(result).not.toBe([1, 3, 5, 7]);
})

Comment on lines 24 to +35
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.

By implementing return [...new Set(array)];, the test will pass correctly, because this implementation always returns a new array.

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.

The concern here is not about your function implementation -- it was correct.

The issue is, how to prepare a test to check if the function is indeed returning a copy of the original array.

With the test you had, a function like this could also pass the test.

function  dedupe(array) {
  const set = new Set(array);
  if (set.size == array.length) return array;
  return [...set];
}  

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.

const input = [1, 3, 5, 7];
const result = dedupe(input);

expect(result).toEqual(input);
expect(result).not.toBe(input);

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.

With this test, there is a chance that, even though result has incorrect elements (for example, []),
the two tests could still pass. Can you figure out why, and then fix the tests accordingly?

Copy link
Copy Markdown
Author

@alizada-dev alizada-dev Mar 24, 2026

Choose a reason for hiding this comment

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

const input = [1, 3, 5, 7];
const result = dedupe(input);

expect(result).toEqual([1, 3, 5, 7]);
expect(result).not.toBe([1, 3, 5, 7]);

Is it alright, this time?

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.

Still not correct. Try looking up the difference between toBe() and toEqual().

// 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("an array with strings strings or numbers", () => {
expect(dedupe([2, 2, 3, 3, 3, "Black", "Black", "Black", "Green"])).toEqual([2, 3, "Black", "Green"]);
})
7 changes: 7 additions & 0 deletions Sprint-1/implement/max.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
function findMax(elements) {
const numbers = elements.filter(Number.isFinite);

if (numbers.length == 0) {
return -Infinity;
}

return Math.max(...numbers);
}

module.exports = findMax;
32 changes: 31 additions & 1 deletion Sprint-1/implement/max.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,58 @@ 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("returns -Infinity for an empty array", () => {
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, should return that number", () => {
expect(findMax([8])).toEqual(8);
expect(findMax([-5])).toEqual(-5);
expect(findMax([0])).toEqual(0);
})

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

test("return the largest number overall", () => {
expect(findMax([-8, -4, 0, 4, 8])).toEqual(8);
expect(findMax([-3, -2, -1, 4, 2, 3])).toEqual(4);
})

// 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 only negative numbers, should return closest to 0", () => {
expect(findMax([-2, -4, -1, -3, -100])).toEqual(-1);
})

// Given an array with decimal numbers
// When passed to the max function
// Then it should return the largest decimal number

test("an array with decimal numbers, should return the largest decimal number", () => {
expect(findMax([0.1, 0.2, 0.9, 0.8, 0.3, 0.7, 0.4, 0.6])).toEqual(0.9);
})

// 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("ignore the non-numeric values", () => {
expect(findMax(["Blue", 3, "400", "2", "White", "Orange", "Pink"])).toEqual(3);
})

Comment thread
cjyuan marked this conversation as resolved.
// 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("an array with only non-number values", () => {
expect(findMax(["Blue", "White", "Orange", "Pink"])).toEqual(-Infinity);
})
5 changes: 5 additions & 0 deletions Sprint-1/implement/sum.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
function sum(elements) {

// const filteredArr = elements.filter(x => typeof x === 'number');
// return filteredArr.reduce((a, b) => a + b, 0);

return elements.filter(Number.isFinite).reduce((a, b) => a + b, 0);
}

module.exports = sum;
28 changes: 27 additions & 1 deletion Sprint-1/implement/sum.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,50 @@ 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("return 0 for an empty array", () => {
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 just one number, return that number", () => {
expect(sum([3])).toEqual(3);
expect(sum([-3])).toEqual(-3);
expect(sum([0])).toEqual(0);
})

// Given an array containing negative numbers
// When passed to the sum function
// Then it should still return the correct total sum

test("return the correct total when passed negative numbers", () => {
expect(sum([-3, -6, -1])).toEqual(-10);
})

// Given an array with decimal/float numbers
// When passed to the sum function
// Then it should return the correct total sum
test("decimal number arrays", () => {
expect(sum([0.2, 0.4, 0.2])).toBeCloseTo(0.8);
expect(sum([1.2, 0.6, 0.005])).toBeCloseTo(1.805);
})
Comment thread
cjyuan marked this conversation as resolved.

// 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("an array with non-numbers", () => {
expect(sum([2, "Blue", 3, "Black", "Green"])).toEqual(5);
})

// 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("an array with non-number values only", () => {
expect(sum([true, false, "Black"])).toEqual(0);
expect(sum([undefined, null, "Black"])).toEqual(0);
})
4 changes: 2 additions & 2 deletions Sprint-1/refactor/includes.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// 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
12 changes: 12 additions & 0 deletions Sprint-1/stretch/aoc-2018-day1/solution.js
Comment thread
cjyuan marked this conversation as resolved.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const fs = require('fs');

const data = fs.readFileSync('input.txt', 'utf-8');

// split by spaces or new lines
// filter out empty values
// convert strings into numbers
const numbers = data.split(/\s+/).filter(Boolean).map(Number);

const sum = numbers.reduce((a, b) => a + b, 0);

console.log(sum); // 529