Skip to content
Closed
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: 23 additions & 2 deletions Sprint-1/fix/median.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,29 @@
// 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];
// Check if the input is a valid array and not empty
if (!Array.isArray(list) || list.length === 0) {
return null;
}
// Filter out non-numeric values
const numericList = list.filter(item => typeof item === 'number' && !isNaN(item));
if (numericList.length === 0) {
return null;
}
// Sort the numeric values
numericList.sort((a, b) => a - b);
const middleIndex = Math.floor(numericList.length / 2);
let median;
if (numericList.length % 2 === 0) {

Choose a reason for hiding this comment

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

This solution is already perfect, but just as an extension, could you show me how you could express this if...else statement using a ternary operator?

Copy link
Author

Choose a reason for hiding this comment

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

Your right we can express the if...else statement using a ternary operator here is how our median calculation will look like using the ternary operator:

numericList.sort((a, b) => a - b);
const middleIndex = Math.floor(numericList.length / 2);

const median = numericList.length % 2 === 0
? (numericList[middleIndex - 1] + numericList[middleIndex]) / 2
: numericList[middleIndex];

Choose a reason for hiding this comment

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

Exactly! Great stuff. In my experience with javascript/typescript, the ternary operator is often preferred over if..else, if there are no "else if" conditions

// If even number of elements, average the two middle values
median = (numericList[middleIndex - 1] + numericList[middleIndex]) / 2;
} else {
// If odd number of elements, take the middle value
median = numericList[middleIndex];
}
if (typeof median !== 'number' || isNaN(median)) {
return null;
}
return median;
}

Expand Down
2 changes: 1 addition & 1 deletion Sprint-1/fix/median.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const calculateMedian = require("./median.js");

describe("calculateMedian", () => {
[
{ input: [1, 2, 3], expected: 2 },
{ 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 },
Expand Down
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) {
// Create a new Set from the array to remove duplicates
// and then convert it back to an array.
return Array.from(new Set(arr));
}

module.exports = dedupe;
25 changes: 24 additions & 1 deletion Sprint-1/implement/dedupe.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,35 @@ 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");
// given an empty array
// when passed to the dedupe function
// then it should return 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 array with no duplicates, it returns a copy of the original array");
// 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 a copy of the original array", () => {
const input = [1, 2, 3];
expect(dedupe(input)).toEqual(input);
});

// 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 removes duplicates preserving the first occurrence");
// given an array with strings or numbers
// when passed to the dedupe function
// then it should remove the duplicate values, preserving the first occurrence of each element
test("given an array with strings or numbers, it removes duplicates preserving the first occurrence", () => {
const input = ['a', 'a', 'b', 'c', 'b'];
const expected = ['a', 'b', 'c'];
expect(dedupe(input)).toEqual(expected);
});
20 changes: 20 additions & 0 deletions Sprint-1/implement/max.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,24 @@
function findMax(elements) {
if (elements.length === 0) {

Choose a reason for hiding this comment

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

You may have heard of the DRY (Don't Repeat Yourself) principle in coding, which emphasises reducing the amount of code that is repeated (such as the same if (elements.length === 0) code you have here and below).
Although these if statements represent different cases (an empty array vs. one with all elements that are non-numeric), they can both be covered just with the if statement on line 8

Copy link
Author

Choose a reason for hiding this comment

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

Removed the first if statement since since the if statement at line 8 covered both cases. and i have updated the code accordingly

return -Infinity; // Return -infinity for an empty array
}

// If non-numeric elements are present, we will ignore them
elements = elements.filter(element => typeof element === 'number');
if (elements.length === 0) {
return -Infinity; // if all elements are non-numeric, return -Infinity
}

// Start with the first numeric element as the maximum
let max = elements[0]; // Assume the first element is the maximum

for (let i = 1; i < elements.length; i++) {
if (elements[i] > max) {
max = elements[i]; // Update max if a larger element is found
}
}

return max; // Return the maximum value found
}

module.exports = findMax;
25 changes: 25 additions & 0 deletions Sprint-1/implement/max.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,52 @@ 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");
// given an empty array
// when passed to the max function
// then it should return -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("given an array with one number, returns that number", () => {
expect(findMax([42])).toBe(42);
});

// 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, 0, 5, 3])).toBe(5);
});

// 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, returns the closest one to zero", () => {
expect(findMax([-10, -5, -3])).toBe(-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 decimal numbers, returns the largest decimal number", () => {
expect(findMax([1.5, 2.3, 0.7])).toBe(2.3);
});

// 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, returns the max and ignores non-numeric values", () => {
expect(findMax(['hey', 10, 'hi', 60, 10])).toBe(60);
});

// 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(['a', 'b', 'c'])).toBe(-Infinity);
});

3 changes: 3 additions & 0 deletions Sprint-1/implement/sum.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
function sum(elements) {
return elements
.filter((el) => typeof el === "number")
.reduce((acc, curr) => acc + curr, 0);
}

module.exports = sum;
19 changes: 18 additions & 1 deletion Sprint-1/implement/sum.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,41 @@ 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([])).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, returns that number", () => {
expect(sum([42])).toBe(42);
});

// 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 correct total sum", () => {
expect(sum([-10, -20, -30])).toBe(-60);
});

// 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 total sum", () => {
expect(sum([10.5, 20.5, 30.5])).toBe(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("given an array containing non-number values, ignores non-numerical values and returns the sum of numerical elements", () => {
expect(sum(['hey', 10, 'hi', 60, 10])).toBe(80);
});

// 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, returns 0", () => {
expect(sum(['hey', 'hi', 'hello'])).toBe(0);
});
12 changes: 10 additions & 2 deletions Sprint-1/refactor/includes.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Refactor the implementation of includes to use a for...of loop

function includes(list, target) {
/*function includes(list, target) {
for (let index = 0; index < list.length; index++) {
const element = list[index];
if (element === target) {
Expand All @@ -9,5 +9,13 @@ function includes(list, target) {
}
return false;
}

*/
function includes(list, target) {
for (const element of list) {
if (element === target) {
return true;
}
}
return false;
}
module.exports = includes;
15 changes: 0 additions & 15 deletions Sprint-2/debug/address.js

This file was deleted.

16 changes: 0 additions & 16 deletions Sprint-2/debug/author.js

This file was deleted.

15 changes: 0 additions & 15 deletions Sprint-2/debug/recipe.js

This file was deleted.

3 changes: 0 additions & 3 deletions Sprint-2/implement/contains.js

This file was deleted.

35 changes: 0 additions & 35 deletions Sprint-2/implement/contains.test.js

This file was deleted.

5 changes: 0 additions & 5 deletions Sprint-2/implement/lookup.js

This file was deleted.

35 changes: 0 additions & 35 deletions Sprint-2/implement/lookup.test.js

This file was deleted.

16 changes: 0 additions & 16 deletions Sprint-2/implement/querystring.js

This file was deleted.

Loading