Skip to content

Commit 1b87061

Browse files
completed-implement
1 parent 716c97b commit 1b87061

6 files changed

Lines changed: 70 additions & 4 deletions

File tree

Sprint-1/implement/dedupe.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,7 @@
1-
function dedupe() {}
1+
function dedupe(arr) {
2+
let newArr = new Set(arr);
3+
return Array.from(newArr)
4+
}
5+
6+
7+
module.exports = dedupe;

Sprint-1/implement/dedupe.test.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,21 @@ 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.todo("given an empty array, it returns an empty array");
20+
test("given an empty array, it should return 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 should return a copy of the original array", () => {
28+
expect(dedupe(['4', 'text'])).toEqual(['4', 'text']);
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 strings or numbers, it should remove the duplicate values, preserving the first occurence of each element", () => {
35+
expect(dedupe([4, '4', 'test', 4, 5, 5, 10, 'test'])).toEqual([4, '4', 'test', 5, 10]);
36+
});

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+
let newList = elements.filter(item => typeof item === "number");
3+
if (newList.length > 0) {
4+
return Math.max(...newList)
5+
}
6+
else return -Infinity
27
}
38

49
module.exports = findMax;

Sprint-1/implement/max.test.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,49 @@ 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.todo("given an empty array, returns -Infinity");
20+
test("given an empty array, returns -Infinity", () => {
21+
expect(findMax([])).toBe(-Infinity);
22+
});
2023

2124
// Given an array with one number
2225
// When passed to the max function
2326
// Then it should return that number
27+
test("given an array with one number, it should return that number", () => {
28+
expect(findMax([2])).toBe(2);
29+
});
2430

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
34+
test("given an array with both positive and negative numbers, it returns the largest number", () => {
35+
expect(findMax([3, -3])).toBe(3);
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 just negative numbers, it returns the closest one to zero", () => {
42+
expect(findMax([-1, -5, -2])).toBe(-1);
43+
});
3244

3345
// Given an array with decimal numbers
3446
// When passed to the max function
3547
// Then it should return the largest decimal number
48+
test("given an array with decimal numbers, it returns the largest decimal number", () => {
49+
expect(findMax([1.5, 20.3, 4.1])).toBe(20.3);
50+
});
3651

3752
// Given an array with non-number values
3853
// When passed to the max function
3954
// Then it should return the max and ignore non-numeric values
55+
test("given an array with non-number values, it returns the max and ignore non-numeric values", () => {
56+
expect(findMax([null, 15, 'text', false, 4, Infinity])).toBe(Infinity);
57+
});
4058

4159
// Given an array with only non-number values
4260
// When passed to the max function
4361
// Then it should return the least surprising value given how it behaves for all other inputs
62+
test("given an array with only non-number values, it returns the least surprising value given how it behaves for all other inputs", () => {
63+
expect(findMax(['text', false, null, undefined, ])).toBe(-Infinity);
64+
});

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+
let counter = 0;
3+
for (const el of elements) {
4+
if (typeof el === 'number') {
5+
counter += el;
6+
}
7+
}
8+
return counter;
29
}
310

411
module.exports = sum;

Sprint-1/implement/sum.test.js

Lines changed: 19 additions & 1 deletion
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.todo("given an empty array, returns 0")
1717

18+
test("given an empty array, returns 0", () => {
19+
expect(sum([])).toBe(0);
20+
});
1821
// Given an array with just one number
1922
// When passed to the sum function
2023
// Then it should return that number
24+
test("given an array with just one number, return that number", () => {
25+
expect(sum([5])).toBe(5);
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 containing negative numbers, return the correct total sum", () => {
32+
expect(sum([5, -3, 9, 2, -1])).toBe(12);
33+
});
2534

2635
// Given an array with decimal/float numbers
2736
// When passed to the sum function
2837
// Then it should return the correct total sum
38+
test("given an array containing decimal/float numbers the correct total sum", () => {
39+
expect(sum([5.1, 3.2, 6.5])).toBe(14.8);
40+
});
2941

3042
// Given an array containing non-number values
3143
// When passed to the sum function
3244
// Then it should ignore the non-numerical values and return the sum of the numerical elements
45+
test("given an array containing non-number values, return the sum of the numerical elements", () => {
46+
expect(sum([5, null, '6', 3])).toBe(8);
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, return return the least surprising value given how it behaves for all other inputs", () => {
53+
expect(sum(['5', undefined, true])).toBe(0);
54+
});

0 commit comments

Comments
 (0)