Skip to content

Commit 309c16f

Browse files
committed
Add Sprint-1 work only
1 parent 4222cda commit 309c16f

8 files changed

Lines changed: 97 additions & 13 deletions

File tree

‎Sprint-1/fix/median.js‎

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,26 @@
66
// or 'list' has mixed values (the function is expected to sort only numbers).
77

88
function calculateMedian(list) {
9-
const middleIndex = Math.floor(list.length / 2);
10-
const median = list.splice(middleIndex, 1)[0];
11-
return median;
9+
if (!Array.isArray(list)) {
10+
return null;
11+
}
12+
13+
const numbers = list.filter(
14+
(value) => typeof value === "number" && !Number.isNaN(value)
15+
);
16+
17+
if (numbers.length === 0) {
18+
return null;
19+
}
20+
21+
const sorted = [...numbers].sort((a, b) => a - b);
22+
const middle = Math.floor(sorted.length / 2);
23+
24+
if (sorted.length % 2 === 0) {
25+
return (sorted[middle - 1] + sorted[middle]) / 2;
26+
}
27+
28+
return sorted[middle];
1229
}
1330

1431
module.exports = calculateMedian;

‎Sprint-1/implement/dedupe.js‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1-
function dedupe() {}
1+
function dedupe(array) {
2+
return [...new Set(array)];
3+
}
4+
5+
module.exports = dedupe;

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

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const dedupe = require("./dedupe.js");
2+
23
/*
34
Dedupe Array
45
@@ -16,13 +17,33 @@ E.g. dedupe([1, 2, 1]) returns [1, 2]
1617
// Given an empty array
1718
// When passed to the dedupe function
1819
// Then it should return an empty array
19-
test.todo("given an empty array, it returns an empty array");
20+
test("given an empty array, it returns an empty array", () => {
21+
expect(dedupe([])).toEqual([]);
22+
});
2023

2124
// Given an array with no duplicates
2225
// When passed to the dedupe function
2326
// Then it should return a copy of the original array
27+
test("given an array with no duplicates, it returns a copy of the original array", () => {
28+
const input = [1, 2, 3];
29+
const result = dedupe(input);
30+
31+
expect(result).toEqual([1, 2, 3]);
32+
expect(result).not.toBe(input);
33+
});
2434

2535
// Given an array of strings or numbers
2636
// When passed to the dedupe function
27-
// Then it should return a new array with duplicates removed while preserving the
37+
// Then it should return a new array with duplicates removed while preserving the
2838
// first occurrence of each element from the original array.
39+
test("removes duplicate strings", () => {
40+
expect(dedupe(["a", "a", "a", "b", "b", "c"])).toEqual(["a", "b", "c"]);
41+
});
42+
43+
test("removes duplicate numbers", () => {
44+
expect(dedupe([5, 1, 1, 2, 3, 2, 5, 8])).toEqual([5, 1, 2, 3, 8]);
45+
});
46+
47+
test("preserves the first occurrence of each element", () => {
48+
expect(dedupe([1, 2, 1])).toEqual([1, 2]);
49+
});

‎Sprint-1/implement/max.js‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
function findMax(elements) {
2+
const numbers = elements.filter(
3+
(element) => typeof element === "number" && !Number.isNaN(element)
4+
);
5+
6+
return Math.max(...numbers);
27
}
38

49
module.exports = findMax;

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

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,48 @@ const findMax = require("./max.js");
1515
// Given an empty array
1616
// When passed to the max function
1717
// Then it should return -Infinity
18-
// Delete this test.todo and replace it with a test.
19-
test.todo("given an empty array, returns -Infinity");
18+
test("given an empty array, returns -Infinity", () => {
19+
expect(findMax([])).toBe(-Infinity);
20+
});
2021

2122
// Given an array with one number
2223
// When passed to the max function
2324
// Then it should return that number
25+
test("given an array with one number, returns that number", () => {
26+
expect(findMax([5])).toBe(5);
27+
});
2428

2529
// Given an array with both positive and negative numbers
2630
// When passed to the max function
2731
// Then it should return the largest number overall
32+
test("returns the largest number from positive and negative numbers", () => {
33+
expect(findMax([-10, 20, -5, 15])).toBe(20);
34+
});
2835

2936
// Given an array with just negative numbers
3037
// When passed to the max function
3138
// Then it should return the closest one to zero
39+
test("returns the largest negative number", () => {
40+
expect(findMax([-10, -5, -20])).toBe(-5);
41+
});
3242

3343
// Given an array with decimal numbers
3444
// When passed to the max function
3545
// Then it should return the largest decimal number
46+
test("returns the largest decimal number", () => {
47+
expect(findMax([1.5, 2.7, 2.3])).toBe(2.7);
48+
});
3649

3750
// Given an array with non-number values
3851
// When passed to the max function
3952
// Then it should return the max and ignore non-numeric values
53+
test("ignores non-number values", () => {
54+
expect(findMax(["hey", 10, "hi", 60, 10])).toBe(60);
55+
});
4056

4157
// Given an array with only non-number values
4258
// When passed to the max function
4359
// Then it should return the least surprising value given how it behaves for all other inputs
60+
test("returns -Infinity when the array contains only non-number values", () => {
61+
expect(findMax(["hey", null, undefined, "hi"])).toBe(-Infinity);
62+
});

‎Sprint-1/implement/sum.js‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
function sum(elements) {
2+
return elements
3+
.filter((element) => typeof element === "number" && !Number.isNaN(element))
4+
.reduce((total, number) => total + number, 0);
25
}
36

47
module.exports = sum;

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

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,44 @@ E.g. sum(['hey', 10, 'hi', 60, 10]), target output: 80 (ignore any non-numerical
88

99
const sum = require("./sum.js");
1010

11-
// Acceptance Criteria:
12-
1311
// Given an empty array
1412
// When passed to the sum function
1513
// Then it should return 0
16-
test.todo("given an empty array, returns 0")
14+
test("given an empty array, returns 0", () => {
15+
expect(sum([])).toBe(0);
16+
});
1717

1818
// Given an array with just one number
1919
// When passed to the sum function
2020
// Then it should return that number
21+
test("given an array with one number, returns that number", () => {
22+
expect(sum([5])).toBe(5);
23+
});
2124

2225
// Given an array containing negative numbers
2326
// When passed to the sum function
2427
// Then it should still return the correct total sum
28+
test("returns the correct sum for negative numbers", () => {
29+
expect(sum([-5, 10, -2])).toBe(3);
30+
});
2531

2632
// Given an array with decimal/float numbers
2733
// When passed to the sum function
2834
// Then it should return the correct total sum
35+
test("returns the correct sum for decimal numbers", () => {
36+
expect(sum([1.5, 2.5, 3])).toBe(7);
37+
});
2938

3039
// Given an array containing non-number values
3140
// When passed to the sum function
3241
// Then it should ignore the non-numerical values and return the sum of the numerical elements
42+
test("ignores non-number values", () => {
43+
expect(sum(["hey", 10, "hi", 60, 10])).toBe(80);
44+
});
3345

3446
// Given an array with only non-number values
3547
// When passed to the sum function
3648
// Then it should return the least surprising value given how it behaves for all other inputs
49+
test("returns 0 when there are only non-number values", () => {
50+
expect(sum(["hey", null, undefined, "hi"])).toBe(0);
51+
});

‎Sprint-1/refactor/includes.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
// Refactor the implementation of includes to use a for...of loop
22

33
function includes(list, target) {
4-
for (let index = 0; index < list.length; index++) {
5-
const element = list[index];
4+
for (const element of list) {
65
if (element === target) {
76
return true;
87
}
98
}
9+
1010
return false;
1111
}
1212

0 commit comments

Comments
 (0)