Skip to content

Commit 04c5cc3

Browse files
committed
Completed exercises in implement directory
1 parent c2c49eb commit 04c5cc3

File tree

6 files changed

+100
-4
lines changed

6 files changed

+100
-4
lines changed

Sprint-1/implement/dedupe.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,23 @@
1-
function dedupe() {}
1+
function dedupe(input) {
2+
// Check if the input is an array
3+
if (!Array.isArray(input)) {
4+
return null;
5+
}
6+
// Check for empty array
7+
if (input.length === 0) {
8+
return [];
9+
}
10+
// New array to hold deduped values
11+
const newArray = [];
12+
// Go through each element in the input array
13+
for (let i = 0; i < input.length; i++) {
14+
const element = input[i];
15+
// If the element is not already in newArray, add it
16+
if (!newArray.includes(element)) {
17+
newArray.push(element);
18+
}
19+
}
20+
return newArray;
21+
}
22+
23+
module.exports = dedupe;

Sprint-1/implement/dedupe.test.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ 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");
2019

2120
// Given an array with no duplicates
2221
// When passed to the dedupe function
@@ -25,3 +24,23 @@ test.todo("given an empty array, it returns an empty array");
2524
// Given an array with strings or numbers
2625
// When passed to the dedupe function
2726
// Then it should remove the duplicate values, preserving the first occurence of each element
27+
28+
describe("dedupe", () => {
29+
[
30+
{ input: [], expected: [] },
31+
{ input: ["apple", "banana", 1, 10], expected: ["apple", "banana", 1, 10] },
32+
{
33+
input: [1, -1, -100, -100, "apple", "apple"],
34+
expected: [1, -1, -100, "apple"],
35+
},
36+
{ input: [-10, -20, -3, -4], expected: [-10, -20, -3, -4] },
37+
{ input: [1.5, 10.5, 0.5], expected: [1.5, 10.5, 0.5] },
38+
{
39+
input: ["apple", "banana", "cherry"],
40+
expected: ["apple", "banana", "cherry"],
41+
},
42+
].forEach(({ input, expected }) =>
43+
it(`returns the deduped array for [${input}]`, () =>
44+
expect(dedupe(input)).toEqual(expected))
45+
);
46+
});

Sprint-1/implement/max.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,16 @@
11
function findMax(elements) {
2+
// Check if input is an array
3+
if (!Array.isArray(elements)) {
4+
return null;
5+
}
6+
// Filter only numeric values
7+
const numericElements = elements.filter((item) => typeof item === "number");
8+
// Check if array is empty after filtering
9+
if (numericElements.length === 0) {
10+
return -Infinity;
11+
}
12+
// Return the maximum value
13+
return Math.max(...numericElements);
214
}
315

416
module.exports = findMax;

Sprint-1/implement/max.test.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ 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");
2019

2120
// Given an array with one number
2221
// When passed to the max function
@@ -41,3 +40,16 @@ test.todo("given an empty array, returns -Infinity");
4140
// Given an array with only non-number values
4241
// When passed to the max function
4342
// Then it should return the least surprising value given how it behaves for all other inputs
43+
44+
describe("findMax", () => {
45+
[
46+
{ input: ["apple", "banana", 1], expected: 1 },
47+
{ input: [1, -1, -100], expected: 1 },
48+
{ input: [-10, -20, -3, -4], expected: -3 },
49+
{ input: [1.5, 10.5, 0.5], expected: 10.5 },
50+
{ input: ["apple", "banana", "cherry"], expected: -Infinity },
51+
].forEach(({ input, expected }) =>
52+
it(`returns the maximum for [${input}]`, () =>
53+
expect(findMax(input)).toEqual(expected))
54+
);
55+
});

Sprint-1/implement/sum.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,23 @@
11
function sum(elements) {
2+
// Check if input is an array
3+
if (!Array.isArray(elements)) {
4+
return null;
5+
}
6+
// Check if array is empty
7+
if (elements.length === 0) {
8+
return 0;
9+
}
10+
// Filter only numeric values
11+
const numericElements = elements.filter((item) => typeof item === "number");
12+
if (numericElements.length === 0) {
13+
return -Infinity;
14+
}
15+
// Calculate the sum of numeric values
16+
let total = 0;
17+
for (let i = 0; i < numericElements.length; i++) {
18+
total += numericElements[i];
19+
}
20+
return total;
221
}
322

423
module.exports = sum;

Sprint-1/implement/sum.test.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ 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")
1716

1817
// Given an array with just one number
1918
// When passed to the sum function
@@ -34,3 +33,16 @@ test.todo("given an empty array, returns 0")
3433
// Given an array with only non-number values
3534
// When passed to the sum function
3635
// Then it should return the least surprising value given how it behaves for all other inputs
36+
37+
describe("sum", () => {
38+
[
39+
{ input: ["apple", "banana", 1, 10], expected: 11 },
40+
{ input: [1, -1, -100], expected: -100 },
41+
{ input: [-10, -20, -3, -4], expected: -37 },
42+
{ input: [1.5, 10.5, 0.5], expected: 12.5 },
43+
{ input: ["apple", "banana", "cherry"], expected: -Infinity },
44+
].forEach(({ input, expected }) =>
45+
it(`returns the sum for [${input}]`, () =>
46+
expect(sum(input)).toEqual(expected))
47+
);
48+
});

0 commit comments

Comments
 (0)