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
29 changes: 25 additions & 4 deletions Sprint-1/fix/median.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,30 @@
// 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;
}
if (!Array.isArray(list) || list.length === 0) {
return null;
}
const numbersOnly = list.filter(element => typeof element === 'number');
if (numbersOnly.length === 0) {
return null;
}

const sortedList = [...numbersOnly].sort((a, b) => a - b);

if (sortedList.length % 2 === 0) {
const middleIndex = Math.floor(sortedList.length / 2);
return (sortedList[middleIndex - 1] + sortedList[middleIndex]) / 2;
}
const middleIndex = Math.floor(sortedList.length / 2);

return sortedList[middleIndex];



}

const salaries = [10, 40, 50, 70, 90]

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

If you have test files, put the tests into those ones rather than leaving them in the main file

const median = calculateMedian(salaries);

module.exports = calculateMedian;

1 change: 1 addition & 0 deletions Sprint-1/fix/median.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,4 @@ describe("calculateMedian", () => {
it(`filters out non-numeric values and calculates the median for [${input}]`, () => expect(calculateMedian(input)).toEqual(expected))
);
});

11 changes: 10 additions & 1 deletion Sprint-1/implement/dedupe.js
Original file line number Diff line number Diff line change
@@ -1 +1,10 @@
function dedupe() {}
function dedupe(list) {
if (!Array.isArray(list)) {

return [];
}

return [...new Set(list)];

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Good use of a set!

}

module.exports = dedupe;
30 changes: 28 additions & 2 deletions Sprint-1/implement/dedupe.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,39 @@ 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.todo("given an empty array, it returns an empty array");

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

// 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.


describe("dedupe function", () => {
test("should remove duplicate elements from an array", () => {
expect(dedupe([1, 2, 2, 3, 1, 4])).toEqual([1, 2, 3, 4]);
expect(dedupe(["apple", "banana", "apple", "orange"])).toEqual([
"apple",
"banana",
"orange",
]);
});

test("should return the same array if there are no duplicates", () => {
expect(dedupe([1, 2, 3])).toEqual([1, 2, 3]);
});

test("should return an empty array if given an empty array", () => {
expect(dedupe([])).toEqual([]);
});

test("should return an empty array if given invalid input (non-arrays)", () => {
expect(dedupe(null)).toEqual([]);
expect(dedupe(undefined)).toEqual([]);
expect(dedupe("not an array")).toEqual([]);
});
});
10 changes: 10 additions & 0 deletions Sprint-1/implement/max.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
function findMax(elements) {
if (!Array.isArray(elements) || elements.length === 0) {

return -Infinity;
}
const numbersOnly = elements.filter(item => typeof item === 'number' && !isNaN(item));
if (numbersOnly.length === 0) {

return -Infinity;
}
return Math.max(...numbersOnly);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Could you maybe try re-implementing this without using the built-in max function?

}

module.exports = findMax;
33 changes: 33 additions & 0 deletions Sprint-1/implement/max.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,36 @@ test.todo("given an empty array, returns -Infinity");
// 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


describe("findMax function", () => {
test("should return the maximum number in an array of positive numbers", () => {
expect(findMax([1, 5, 3, 9, 2])).toBe(9);
});

test("should return the maximum number in an array with negative numbers", () => {
expect(findMax([-10, -3, -50, -1])).toBe(-1);
});

test("should handle arrays with mixed types and ignore non-numbers", () => {
expect(findMax([1, "apple", 5, null, true, 3])).toBe(5);
});

test("should ignore NaN values", () => {
expect(findMax([1, NaN, 10, 2])).toBe(10);
});

test("should return -Infinity if given an empty array", () => {
expect(findMax([])).toBe(-Infinity);
});

test("should return -Infinity if given an array with no valid numbers", () => {
expect(findMax(["a", "b", null, NaN])).toBe(-Infinity);
});

test("should return -Infinity for non-array inputs", () => {
expect(findMax(null)).toBe(-Infinity);
expect(findMax(undefined)).toBe(-Infinity);
expect(findMax("hello")).toBe(-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) {
if (!Array.isArray(elements) || elements.length === 0) {
return 0;
}

const numbersOnly = elements.filter(item => typeof item === "number" && !isNaN(item));

if (numbersOnly.length === 0) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

If you have 0 elements, do you need this if condition here?

return 0;
}

return numbersOnly.reduce((acc, curr) => acc + curr, 0);

}

module.exports = sum;

41 changes: 40 additions & 1 deletion Sprint-1/implement/sum.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ 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");

// Given an array with just one number
// When passed to the sum function
Expand All @@ -34,3 +34,42 @@ test.todo("given an empty array, returns 0")
// 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

describe("sum function", () => {
// Given an empty array
test("given an empty array, returns 0", () => {
expect(sum([])).toBe(0);
});

// Given an array with just one number
test("given an array with just one number, returns that number", () => {
expect(sum([42])).toBe(42);
});

// Given an array containing negative numbers
test("given an array containing negative numbers, returns the correct total sum", () => {
expect(sum([10, -5, 20, -15])).toBe(10);
});

// Given an array with decimal/float numbers
test("given an array with decimal/float numbers, returns the correct total sum", () => {
expect(sum([1.5, 2.25, 3.25])).toBe(7);
});

// Given an array containing non-number values
test("given an array containing non-number values, ignores non-numerical values", () => {
expect(sum(["hey", 10, "hi", 60, 10])).toBe(80);
expect(sum([10, true, null, undefined, NaN, 20])).toBe(30);
});

// Given an array with only non-number values
test("given an array with only non-number values, returns 0", () => {
expect(sum(["apple", "banana", true, null])).toBe(0);
});

// Edge case: Non-array inputs
test("given non-array inputs, returns 0", () => {
expect(sum(null)).toBe(0);
expect(sum("not an array")).toBe(0);
});
});
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.

Loading