Skip to content

Commit fafc331

Browse files
committed
sprint 1 sorted
1 parent ce620b7 commit fafc331

File tree

8 files changed

+112
-10
lines changed

8 files changed

+112
-10
lines changed

Sprint-1/fix/median.js

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,27 @@
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 numericValues = list.filter(item => typeof item === 'number' && !isNaN(item));
14+
15+
if (numericValues.length === 0) {
16+
return null;
17+
}
18+
19+
const sortedList = [...numericValues].sort((a, b) => a - b);
20+
21+
const length = sortedList.length;
22+
const middleIndex = Math.floor(length / 2);
23+
24+
if (length % 2 === 1) {
25+
return sortedList[middleIndex];
26+
}
27+
28+
return (sortedList[middleIndex - 1] + sortedList[middleIndex]) / 2;
1229
}
1330

1431
module.exports = calculateMedian;
32+

Sprint-1/implement/dedupe.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,16 @@
1-
function dedupe() {}
1+
function dedupe(elements) {
2+
if (!elements || elements.length === 0) {
3+
return [];
4+
}
5+
6+
const seen = new Set();
7+
return elements.filter(element => {
8+
if (seen.has(element)) {
9+
return false;
10+
}
11+
seen.add(element);
12+
return true;
13+
});
14+
}
15+
16+
module.exports = dedupe;

Sprint-1/implement/dedupe.test.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,23 @@ E.g. dedupe([1, 2, 1]) target output: [1, 2]
1616
// Given an empty array
1717
// When passed to the dedupe function
1818
// Then it should return an empty array
19-
test.todo("given an empty array, it returns an empty array");
19+
test("given an empty array, it returns an empty array", () => {
20+
expect(dedupe([])).toEqual([]);
21+
});
2022

2123
// Given an array with no duplicates
2224
// When passed to the dedupe function
2325
// Then it should return a copy of the original array
26+
test("given an array with no duplicates, returns a copy", () => {
27+
expect(dedupe([1, 2, 3, 4])).toEqual([1, 2, 3, 4]);
28+
expect(dedupe(['a', 'b', 'c'])).toEqual(['a', 'b', 'c']);
29+
});
2430

2531
// Given an array with strings or numbers
2632
// When passed to the dedupe function
2733
// Then it should remove the duplicate values, preserving the first occurence of each element
34+
test("given an array with duplicates, removes them preserving first occurrence", () => {
35+
expect(dedupe(['a','a','a','b','b','c'])).toEqual(['a','b','c']);
36+
expect(dedupe([5, 1, 1, 2, 3, 2, 5, 8])).toEqual([5, 1, 2, 3, 8]);
37+
expect(dedupe([1, 2, 1])).toEqual([1, 2]);
38+
});

Sprint-1/implement/max.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11
function findMax(elements) {
2+
const numbers = elements.filter(element => typeof element === 'number');
3+
4+
if (numbers.length === 0) {
5+
return -Infinity;
6+
}
7+
8+
return Math.max(...numbers);
29
}
310

411
module.exports = findMax;

Sprint-1/implement/max.test.js

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,53 @@ 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([42])).toBe(42);
27+
expect(findMax([-5])).toBe(-5);
28+
});
2429

2530
// Given an array with both positive and negative numbers
2631
// When passed to the max function
2732
// Then it should return the largest number overall
33+
test("given an array with positive and negative numbers, returns the largest", () => {
34+
expect(findMax([30, 50, 10, 40])).toBe(50);
35+
expect(findMax([-10, 5, -20, 15])).toBe(15);
36+
});
2837

2938
// Given an array with just negative numbers
3039
// When passed to the max function
3140
// Then it should return the closest one to zero
41+
test("given an array with only negative numbers, returns closest to zero", () => {
42+
expect(findMax([-10, -5, -20])).toBe(-5);
43+
expect(findMax([-1, -2, -3])).toBe(-1);
44+
});
3245

3346
// Given an array with decimal numbers
3447
// When passed to the max function
3548
// Then it should return the largest decimal number
49+
test("given an array with decimal numbers, returns the largest", () => {
50+
expect(findMax([1.5, 2.7, 1.9])).toBe(2.7);
51+
expect(findMax([0.1, 0.3, 0.2])).toBe(0.3);
52+
});
3653

3754
// Given an array with non-number values
3855
// When passed to the max function
3956
// Then it should return the max and ignore non-numeric values
57+
test("given an array with non-numeric values, ignores them", () => {
58+
expect(findMax(['hey', 10, 'hi', 60, 10])).toBe(60);
59+
expect(findMax([true, 5, null, 25, 'hello'])).toBe(25);
60+
});
4061

4162
// Given an array with only non-number values
4263
// When passed to the max function
4364
// Then it should return the least surprising value given how it behaves for all other inputs
65+
test("given an array with only non-numeric values, returns -Infinity", () => {
66+
expect(findMax(['hello', 'world', true, null])).toBe(-Infinity);
67+
});

Sprint-1/implement/sum.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11
function sum(elements) {
2+
if (elements.length === 0) {
3+
return 0;
4+
}
5+
6+
return elements
7+
.filter(element => typeof element === 'number')
8+
.reduce((total, num) => total + num, 0);
29
}
310

411
module.exports = sum;

Sprint-1/implement/sum.test.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,45 @@ const sum = require("./sum.js");
1313
// Given an empty array
1414
// When passed to the sum function
1515
// Then it should return 0
16-
test.todo("given an empty array, returns 0")
16+
test("given an empty array, returns 0", () => {
17+
expect(sum([])).toBe(0);
18+
});
1719

1820
// Given an array with just one number
1921
// When passed to the sum function
2022
// Then it should return that number
23+
test("given an array with one number, returns that number", () => {
24+
expect(sum([5])).toBe(5);
25+
expect(sum([42])).toBe(42);
26+
});
2127

2228
// Given an array containing negative numbers
2329
// When passed to the sum function
2430
// Then it should still return the correct total sum
31+
test("given an array with negative numbers, returns correct sum", () => {
32+
expect(sum([1, -2, 3])).toBe(2);
33+
expect(sum([-5, -10, -15])).toBe(-30);
34+
});
2535

2636
// Given an array with decimal/float numbers
2737
// When passed to the sum function
2838
// Then it should return the correct total sum
39+
test("given an array with decimal numbers, returns correct sum", () => {
40+
expect(sum([1.5, 2.5, 3.0])).toBe(7);
41+
expect(sum([0.1, 0.2, 0.3])).toBeCloseTo(0.6);
42+
});
2943

3044
// Given an array containing non-number values
3145
// When passed to the sum function
3246
// Then it should ignore the non-numerical values and return the sum of the numerical elements
47+
test("given an array with non-numeric values, ignores them", () => {
48+
expect(sum(['hey', 10, 'hi', 60, 10])).toBe(80);
49+
expect(sum([true, 5, null, 15, 'hello'])).toBe(20);
50+
});
3351

3452
// Given an array with only non-number values
3553
// When passed to the sum function
3654
// Then it should return the least surprising value given how it behaves for all other inputs
55+
test("given an array with only non-numeric values, returns 0", () => {
56+
expect(sum(['hello', 'world', true, null])).toBe(0);
57+
});

Sprint-1/refactor/includes.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
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
}

0 commit comments

Comments
 (0)