Skip to content

Commit 0c5f55e

Browse files
committed
fixed dedupe function and wrote tests
1 parent 08758cb commit 0c5f55e

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

Sprint-1/implement/dedupe.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,13 @@
1-
function dedupe() {}
1+
function dedupe(array) {
2+
let arr = array.filter((item, index) => {
3+
4+
// indexOf() returns the first index where that value appears in the array.
5+
if (array.indexOf(item) === index) {
6+
return item;
7+
}
8+
})
9+
10+
return arr;
11+
}
12+
13+
module.exports = dedupe;

Sprint-1/implement/dedupe.test.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,23 @@ 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+
20+
test("return an empty array for 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
2427

28+
test("if no duplicates, return the copy of the original array", () => {
29+
expect(dedupe([1, 3, 5, 7])).toEqual([1, 3, 5, 7]);
30+
})
31+
2532
// Given an array with strings or numbers
2633
// When passed to the dedupe function
2734
// Then it should remove the duplicate values, preserving the first occurence of each element
35+
36+
test("an array with strings strings or numbers", () => {
37+
expect(dedupe([2, 2, 3, 3, 3, "Black", "Black", "Black", "Green"])).toEqual([2, 3, "Black", "Green"]);
38+
})

0 commit comments

Comments
 (0)