Skip to content

Commit c96038b

Browse files
committed
updated the task based on reviewes feedback
1 parent e0ff101 commit c96038b

6 files changed

Lines changed: 16 additions & 28 deletions

File tree

Sprint-1/implement/dedupe.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
function dedupe(array) {
22
const result = [];
33

4-
for (let i = 0; i < array.length; i++)
5-
if (!result.includes(array[i])) {
6-
result.push(array[i]);
4+
for (const item of array) {
5+
if (!result.includes(item)) {
6+
result.push(item);
77
}
8-
9-
return result;
8+
}
109
}
1110

1211
module.exports = dedupe;

Sprint-1/implement/dedupe.test.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ test("given an empty array, it returns an empty array", () => {
2828
test("given an array with no duplicates, it returns a copy of the original array", () => {
2929
const input = [1, 2, 3];
3030
const expectedOutput = [1, 2, 3];
31-
expect(dedupe(input)).toEqual(expectedOutput);
31+
const result = dedupe(input);
32+
33+
expect(result).toEqual(expectedOutput);
34+
expect(result).not.toBe(input);
3235
});
3336

3437
// Given an array with strings or numbers

Sprint-1/implement/max.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ function findMax(elements) {
22
let max = -Infinity;
33

44
for (let i = 0; i < elements.length; i++) {
5-
if (typeof elements[i] === "number" && elements[i] > max) {
5+
if (
6+
typeof elements[i] === "number" &&
7+
!Number.isNaN(elements[i]) &&
8+
elements[i] > max
9+
) {
610
max = elements[i];
711
}
812
}

Sprint-1/implement/max.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ test("given an array with decimal numbers, it returns the largest decimal number
6262
// When passed to the max function
6363
// Then it should return the max and ignore non-numeric values
6464
test("given an array with non-number values, it returns the max and ignore non-numeric values", () => {
65-
const input = [10, "hello", 20, null, 5];
65+
const input = [10, "300", "hello", 20, null, 5];
6666
const expectedOutput = 20;
6767
expect(findMax(input)).toBe(expectedOutput);
6868
});

Sprint-1/implement/sum.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ function sum(elements) {
22
let total = 0;
33

44
for (let i = 0; i < elements.length; i++) {
5-
if (typeof elements[i] === "number") {
5+
if (Number.isFinite(elements[i])) {
66
total += elements[i];
77
}
88
}

Sprint-1/implement/sum.test.js

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,7 @@ test("given an array containing negative numbers, it returns the correct total s
4343
test("given an array with decimal numbers, it returns the correct total sum", () => {
4444
const input = [1.5, 2.3, 0.7];
4545
const expectedOutput = 4.5;
46-
expect(sum(input)).toBe(expectedOutput);
47-
});
48-
49-
// Given an array with decimal/float numbers
50-
// When passed to the sum function
51-
// Then it should return the correct total sum
52-
test("given an array with decimal numbers, it returns the correct total sum", () => {
53-
const input = [1.5, 2.3, 0.7];
54-
const expectedOutput = 4.5;
55-
expect(sum(input)).toBe(expectedOutput);
46+
expect(sum(input)).toBeCloseTo(expectedOutput);
5647
});
5748

5849
// Given an array containing non-number values
@@ -64,15 +55,6 @@ test("given an array containing non-number values, it ignores them and returns t
6455
expect(sum(input)).toBe(expectedOutput);
6556
});
6657

67-
// Given an array containing non-number values
68-
// When passed to the sum function
69-
// Then it should ignore the non-numerical values and return the sum of the numerical elements
70-
test("given an array containing non-number values, it ignores them and returns the sum of the numerical elements", () => {
71-
const input = ["hello", "world"];
72-
const expectedOutput = 0;
73-
expect(sum(input)).toBe(expectedOutput);
74-
});
75-
7658
// Given an array with only non-number values
7759
// When passed to the sum function
7860
// Then it should return the least surprising value given how it behaves for all other inputs

0 commit comments

Comments
 (0)