Skip to content

Commit 552c695

Browse files
committed
dedupe.js + tests completed
1 parent 8e2d553 commit 552c695

2 files changed

Lines changed: 27 additions & 3 deletions

File tree

‎Sprint-1/implement/dedupe.js‎

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

‎Sprint-1/implement/dedupe.test.js‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,24 @@ E.g. dedupe([1, 2, 1]) returns [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, should return a copy of the original array", () => {
27+
const original = ["a", 11, "55"];
28+
const expected = dedupe(original);
29+
expect(expected).toEqual(original);
30+
expect(expected).not.toBe(original);
31+
});
2432

2533
// Given an array of strings or numbers
2634
// When passed to the dedupe function
27-
// Then it should return a new array with duplicates removed while preserving the
35+
// Then it should return a new array with duplicates removed while preserving the
2836
// first occurrence of each element from the original array.
37+
test("given an array of strings and numbers, should return a new array with duplicates removed", () => {
38+
expect(dedupe(["a", "a", 33, 33, 1])).toEqual(["a", 33, 1]);
39+
});

0 commit comments

Comments
 (0)