Skip to content

Commit 654c7a9

Browse files
committed
chore: Solutions for Sprint 1
1 parent 461add5 commit 654c7a9

14 files changed

Lines changed: 171 additions & 32 deletions

File tree

‎Sprint-1/fix/median.js‎

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,27 @@
22
// Start by running the tests for this function
33
// If you're in the week-1 directory, you can run npm test -- fix to run the tests in the fix directory
44

5+
// Old Code:
6+
7+
// function calculateMedian(list) {
8+
// const middleIndex = Math.floor(list.length / 2);
9+
// const median = list.splice(middleIndex, 1)[0];
10+
// return median;
11+
// }
12+
13+
// Explanation:
14+
// .splice() is a mutating method, so it updates the array in place.
15+
// to prevent this, you could take a copy of the array and use .splice(),
16+
// or you could use bracket notation to element at the desired index
17+
518
function calculateMedian(list) {
619
const middleIndex = Math.floor(list.length / 2);
7-
const median = list.splice(middleIndex, 1)[0];
8-
return median;
20+
21+
if (list.length % 2 === 0) { // use the remainder operator (%) to check if the array length is even
22+
return (list[middleIndex - 1] + list[middleIndex]) / 2;
23+
}
24+
25+
return list[middleIndex];
926
}
1027

1128
module.exports = calculateMedian;

‎Sprint-1/fix/median.test.js‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,17 @@
77
const calculateMedian = require("./median.js");
88

99
describe("calculateMedian", () => {
10-
test("returns the median for odd length array", () => {
10+
it("returns the median for odd length array", () => {
1111
expect(calculateMedian([1, 2, 3])).toBe(2);
1212
expect(calculateMedian([1, 2, 3, 4, 5])).toBe(3);
1313
});
1414

15-
test("returns the average of middle values for even length array", () => {
15+
it("returns the average of middle values for even length array", () => {
1616
expect(calculateMedian([1, 2, 3, 4])).toBe(2.5);
1717
expect(calculateMedian([1, 2, 3, 4, 5, 6])).toBe(3.5);
1818
});
1919

20-
test("doesn't modify the input", () => {
20+
it("doesn't modify the input", () => {
2121
const list = [1, 2, 3];
2222
calculateMedian(list);
2323

‎Sprint-1/implement/dedupe.js‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,10 @@
1-
function dedupe() {}
1+
// Explanation:
2+
3+
// All elements in a set are unique, so creating a set from an array removes duplicates - new Set(list)
4+
// Creating a new array and using the spread operator (3 dots, ...) will destruct the values of the set back to the array
5+
6+
function dedupe(list) {
7+
return [...new Set(list)];
8+
}
9+
10+
module.exports = dedupe;

‎Sprint-1/implement/dedupe.test.js‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ E.g. dedupe([5, 1, 1, 2, 3, 2, 5, 8]) target output: [5, 1, 2, 3, 8]
1515
// Given an empty array
1616
// When passed to the dedupe function
1717
// Then it should return an empty array
18-
test.todo("given an empty array, it returns an empty array");
1918

2019
// Given an array with no duplicates
2120
// When passed to the dedupe function
@@ -24,3 +23,20 @@ test.todo("given an empty array, it returns an empty array");
2423
// Given an array with strings or numbers
2524
// When passed to the dedupe function
2625
// Then it should remove the duplicate values
26+
27+
describe("dedupe", () => {
28+
it("returns the array without duplicates", () => {
29+
expect(dedupe(['a','a','a','b','b','c'])).toStrictEqual(['a','b','c']);
30+
expect(dedupe([5, 1, 1, 2, 3, 2, 5, 8])).toStrictEqual([5, 1, 2, 3, 8]);
31+
});
32+
33+
it("returns the original array when the original array does not contain duplicates", () => {
34+
expect(dedupe(['a','b','c'])).toStrictEqual(['a','b','c']);
35+
expect(dedupe([5, 1, 2, 3, 8])).toStrictEqual([5, 1, 2, 3, 8]);
36+
});
37+
38+
it("returns an array when an empty array is passed to the function", () => {
39+
expect(dedupe([])).toStrictEqual([]);
40+
});
41+
});
42+

‎Sprint-1/implement/max.js‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Explanation:
2+
3+
// list.filter(value => typeof value === "number") checks the value type (number, string etc)
4+
// to remove anything that is not a number
5+
// numericValues.length === 0 ? -Infinity : Math.max(...numericValues) is a ternary operator expression
6+
// if the condition (whatever is before the question mark, in this case (numericValues.length === 0)) is true
7+
// then -Infinity will be returned, if the condition is false Math.max(...numericValues) will be returned
8+
// Math.Max() finds the largest number in the given parameters
9+
10+
11+
function getLargestNumber(list) {
12+
const numericValues = list.filter(value => typeof value === "number");
13+
14+
return numericValues.length === 0 ? -Infinity : Math.max(...numericValues);
15+
}
16+
17+
module.exports = getLargestNumber;

‎Sprint-1/implement/max.test.js‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ E.g. max(['hey', 10, 'hi', 60, 10]), target output: 60 (sum ignores any non-nume
99
// Given an empty array
1010
// When passed to the max function
1111
// Then it should return -Infinity
12+
1213
test.todo("given an empty array, returns -Infinity");
1314

1415
// Given an array with one number
@@ -26,3 +27,27 @@ test.todo("given an empty array, returns -Infinity");
2627
// Given an array with non-number values
2728
// When passed to the max function
2829
// Then it should return the max and ignore non-numeric values
30+
31+
const getLargestNumber = require("./max");
32+
33+
describe("getLargestNumber", () => {
34+
it("returns negative infinity when the array is empty", () => {
35+
expect(getLargestNumber([])).toEqual(-Infinity);
36+
});
37+
38+
it("returns the only value when the array contains only 1 number", () => {
39+
expect(getLargestNumber([5])).toEqual(5);
40+
});
41+
42+
it("returns the largest number when the array contains positive and negative numbers", () => {
43+
expect(getLargestNumber([43, 75, 21, -64, -97])).toEqual(75);
44+
});
45+
46+
it("returns the largest number when the array contains strings, and positive and negative numbers", () => {
47+
expect(getLargestNumber([-54, 71, 342, -987321, "test", "again"])).toEqual(342);
48+
});
49+
50+
it("returns the largest decimal number when the array contains decimal numbers", () => {
51+
expect(getLargestNumber([14.7, 1.43, 87.45, 6.432])).toEqual(87.45);
52+
});
53+
});

‎Sprint-1/implement/sum.js‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Explanation:
2+
3+
// list.filter(value => typeof value === "number") checks the value type (number, string etc)
4+
// to remove anything that is not a number
5+
// reduce((agg, value) => agg + value, 0) uses an aggregator (agg) to keep an overall tally while iterating the array
6+
// the second argument in the reduce function, in this case a 0, is the default value of the aggregator
7+
8+
function sum(list) {
9+
return list.filter(value => typeof value === "number").reduce((agg, value) => agg + value, 0);
10+
}
11+
12+
module.exports = sum;

‎Sprint-1/implement/sum.test.js‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,27 @@ E.g. sum(['hey', 10, 'hi', 60, 10]), target output: 80 (ignore any non-numerical
2727
// Given an array containing non-number values
2828
// When passed to the sum function
2929
// Then it should ignore the non-numerical values and return the sum of the numerical elements
30+
31+
const sum = require("./sum");
32+
33+
describe("sum", () => {
34+
it("returns 0 when the array is empty", () => {
35+
expect(sum([])).toEqual(0);
36+
});
37+
38+
it("returns the only number when the array contains one number", () => {
39+
expect(sum([8])).toEqual(8);
40+
});
41+
42+
it("returns the correct sum value when the array contains a sequence of numbers", () => {
43+
expect(sum([-10, 40, 50])).toEqual(80);
44+
expect(sum([10, 20, 30])).toEqual(60);
45+
expect(sum([1.1, 2.2, 3.3])).toEqual(6.6);
46+
});
47+
48+
it("returns the correct sum value when the array contains a sequence of numbers and strings", () => {
49+
expect(sum([-10, 40, 50, "testing"])).toEqual(80);
50+
expect(sum([10, 20, 30, "testing"])).toEqual(60);
51+
expect(sum([1.1, 2.2, 3.3, "testing"])).toEqual(6.6);
52+
});
53+
});

‎Sprint-1/refactor/find.js‎

Lines changed: 0 additions & 13 deletions
This file was deleted.

‎Sprint-1/refactor/includes.js‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Refactor the implementation of find to use a for...of loop
2+
3+
// function includes(list, target) {
4+
// for (let index = 0; index < list.length; index++) {
5+
// const element = list[index];
6+
// if (element === target) {
7+
// return index;
8+
// }
9+
// }
10+
// return -1;
11+
// }
12+
13+
function includes(list, target) {
14+
for (const value of list) {
15+
if (value === target) {
16+
return list.indexOf(value);
17+
}
18+
}
19+
20+
return -1;
21+
}
22+
23+
module.exports = includes;

0 commit comments

Comments
 (0)