Skip to content

Commit 9c8d740

Browse files
Sprint-1:data group
1 parent ce620b7 commit 9c8d740

File tree

11 files changed

+1669
-952
lines changed

11 files changed

+1669
-952
lines changed

Sprint-1/fix/median.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,18 @@
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)) return null;
10+
const numbers = list.filter(val => typeof val === 'number' && !isNaN(val));
11+
12+
if (numbers.length === 0) return null;
13+
const sorted = [...numbers].sort((a, b) => a - b);
14+
const mid = Math.floor(sorted.length / 2);
15+
16+
if (sorted.length % 2 === 0) {
17+
return (sorted[mid - 1] + sorted[mid]) / 2;
18+
} else {
19+
return sorted[mid];
20+
}
1221
}
1322

14-
module.exports = calculateMedian;
23+
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: 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 same array", () => {
27+
expect(dedupe([1, 2, 3])).toEqual([1, 2, 3]);
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("removes duplicates and keeps 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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
function findMax(elements) {
2+
if (!Array.isArray(elements)) return -Infinity;
3+
4+
const numbers = elements.filter(item => typeof item === "number");
5+
if (numbers.length === 0) return -Infinity;
6+
7+
return Math.max(...numbers);
28
}
39

4-
module.exports = findMax;
10+
module.exports = findMax;

Sprint-1/implement/max.test.js

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

2123
// Given an array with one number
2224
// When passed to the max function
2325
// Then it should return that number
26+
test("given an array with one number, returns that number", () => {
27+
expect(findMax([60])).toBe(60);
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 positive and negative numbers, returns the largest", () => {
34+
expect(findMax([-10, 0, 15, -4, 7])).toBe(15);
35+
});
2836

2937
// Given an array with just negative numbers
3038
// When passed to the max function
3139
// Then it should return the closest one to zero
40+
test("given only negative numbers, returns the least negative", () => {
41+
expect(findMax([-50, -10, -30])).toBe(-10);
42+
});
3243

3344
// Given an array with decimal numbers
3445
// When passed to the max function
3546
// Then it should return the largest decimal number
47+
test("given decimal numbers, returns the largest one", () => {
48+
expect(findMax([6.2, 7.5, 2.7])).toBe(7.5);
49+
});
3650

3751
// Given an array with non-number values
3852
// When passed to the max function
3953
// Then it should return the max and ignore non-numeric values
54+
test("given an array with non-number values, ignores non-number values and returns the max number", () => {
55+
expect(findMax(['hey', 10, 'hi', 60, 10])).toBe(60);
56+
});
4057

41-
// Given an array with only non-number values
58+
// Given an array with only non-number values
4259
// When passed to the max function
4360
// Then it should return the least surprising value given how it behaves for all other inputs
61+
test("given an array with only non-number values, returns -Infinity", () => {
62+
expect(findMax(['hey', 'hi', 'hello'])).toBe(-Infinity);
63+
});

Sprint-1/implement/sum.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
function sum(elements) {
2+
if (!Array.isArray(elements))
3+
return 0;
4+
5+
return elements
6+
.filter(item => typeof item === "number")
7+
.reduce((total, num) => total + num, 0);
28
}
39

4-
module.exports = sum;
10+
module.exports = sum;

Sprint-1/implement/sum.test.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,42 @@ 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([60])).toBe(60);
25+
});
2126

2227
// Given an array containing negative numbers
2328
// When passed to the sum function
2429
// Then it should still return the correct total sum
30+
test("given an array with negative numbers, returns the correct total sum", () => {
31+
expect(sum([-10, -15, -4])).toBe(-29);
32+
});
2533

2634
// Given an array with decimal/float numbers
2735
// When passed to the sum function
2836
// Then it should return the correct total sum
37+
test("given an array with decimal numbers, returns the correct total sum", () => {
38+
expect(sum([6.2, 7.5, 2.7])).toBe(16.4);
39+
});
2940

3041
// Given an array containing non-number values
3142
// When passed to the sum function
32-
// Then it should ignore the non-numerical values and return the sum of the numerical elements
43+
// Then it should ignore the non-numerical values and return
44+
// the sum of the numerical elements
45+
test("given an array with non-number values, ignores non-number values and returns the sum", () => {
46+
expect(sum(['hey', 10, 'hi', 60, 10])).toBe(80);
47+
});
3348

3449
// Given an array with only non-number values
3550
// When passed to the sum function
3651
// Then it should return the least surprising value given how it behaves for all other inputs
52+
test("given an array with only non-number values, returns 0", () => {
53+
expect(sum(['hey', 'hi', 'hello'])).toBe(0);
54+
});

0 commit comments

Comments
 (0)