Skip to content

Commit 7bc29b0

Browse files
committed
completed implement
1 parent 96d077b commit 7bc29b0

File tree

6 files changed

+89
-5
lines changed

6 files changed

+89
-5
lines changed

Sprint-1/implement/dedupe.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,12 @@
1-
function dedupe() {}
1+
function dedupe(elements) {
2+
const result = [];
3+
for (let i = 0; i < elements.length; i++) {
4+
if (!result.includes(elements[i])) {
5+
result.push(elements[i]);
6+
}
7+
}
8+
9+
return result;
10+
}
11+
12+
module.exports = dedupe;

Sprint-1/implement/dedupe.test.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ 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

Sprint-1/implement/max.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,18 @@
11
function findMax(elements) {
2-
}
2+
const numbersOnly = elements.filter((e) => typeof e === "number");
3+
4+
if (numbersOnly.length === 0) {
5+
return -Infinity;
6+
}
7+
8+
let max = numbersOnly[0];
39

10+
for (let i = 1; i < numbersOnly.length; i++) {
11+
if (numbersOnly[i] > max) {
12+
max = numbersOnly[i];
13+
}
14+
}
15+
16+
return max;
17+
}
418
module.exports = findMax;

Sprint-1/implement/max.test.js

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,54 @@ 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
2426

27+
test("given an array with one number, returns that number", () => {
28+
expect(findMax([7])).toBe(7);
29+
});
30+
2531
// Given an array with both positive and negative numbers
2632
// When passed to the max function
2733
// Then it should return the largest number overall
2834

35+
test("given positive and negative numbers, returns the largest number", () => {
36+
expect(findMax([-3, 7, -2, 5])).toBe(7);
37+
});
38+
2939
// Given an array with just negative numbers
3040
// When passed to the max function
3141
// Then it should return the closest one to zero
3242

43+
test("given an array with only negative numbers, returns the closest to zero", () => {
44+
expect(findMax([-10, -3, -7])).toBe(-3);
45+
});
46+
3347
// Given an array with decimal numbers
3448
// When passed to the max function
3549
// Then it should return the largest decimal number
3650

51+
test("given an array with decimal numbers, returns the clargest decimal number", () => {
52+
expect(findMax([2.7, 6.9, 10.1])).toBe(10.1);
53+
});
54+
3755
// Given an array with non-number values
3856
// When passed to the max function
3957
// Then it should return the max and ignore non-numeric values
4058

59+
test("given an array with non-number values, returns the max and ignore non-numeric values", () => {
60+
expect(findMax([3, "dogs", 1, "cat", null])).toBe(3);
61+
});
62+
4163
// Given an array with only non-number values
4264
// When passed to the max function
4365
// Then it should return the least surprising value given how it behaves for all other inputs
66+
67+
test("given an array with only non-number values, returns -Infinity", () => {
68+
expect(findMax(["a", null, true, undefined])).toBe(-Infinity);
69+
});

Sprint-1/implement/sum.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
11
function sum(elements) {
2+
let total = 0;
3+
4+
for (let i = 0; i < elements.length; i++) {
5+
if (typeof elements[i] === "number") {
6+
total += elements[i];
7+
}
8+
}
9+
10+
return total;
211
}
312

413
module.exports = sum;

Sprint-1/implement/sum.test.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,46 @@ 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
2123

24+
test("given an array with one number, returns that number", () => {
25+
expect(sum([42])).toBe(42);
26+
});
27+
2228
// Given an array containing negative numbers
2329
// When passed to the sum function
2430
// Then it should still return the correct total sum
2531

32+
test("given an array containing negative numbers", () => {
33+
expect(sum([-5, -6, -1])).toBe(-12);
34+
});
35+
2636
// Given an array with decimal/float numbers
2737
// When passed to the sum function
2838
// Then it should return the correct total sum
2939

40+
test("given an array with decimal/float numbers, returns the correct total sum", () => {
41+
expect(sum([2.75, -0.3, 0.0002])).toBeCloseTo(2.4502);
42+
});
43+
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
3347

48+
test("given an array containing non-number values, ignore the non-numerical values and return the sum of the numerical elements", () => {
49+
expect(sum(["hello", 6, true, 14])).toBe(20);
50+
});
51+
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+
56+
test("given an array with only non-number values, returns 0", () => {
57+
expect(sum(["hello", undefined, "today", true])).toBe(0);
58+
});

0 commit comments

Comments
 (0)