Skip to content
This repository has been archived by the owner on Dec 18, 2024. It is now read-only.

NW6 | Pedram Amani | JS2-Module | Week1 #155

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
JS2-week1 | dedupe.js & dedupe.test.js done
  • Loading branch information
pedram-am committed Jan 2, 2024
commit 15c0c1a5df770b05115053a6df9515f079b3b308
16 changes: 15 additions & 1 deletion week-1/implement/dedupe.js
Original file line number Diff line number Diff line change
@@ -1 +1,15 @@
function dedupe() {}
function dedupe(arr) {
const deduplicatedArray = [];

for (const item of arr) {
if (!deduplicatedArray.includes(item)) {
deduplicatedArray.push(item);
}
}

return deduplicatedArray;
}

console.log(dedupe(["a", "a", "a", "b", "b", "c"]));

module.exports = dedupe;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work mate!

15 changes: 14 additions & 1 deletion week-1/implement/dedupe.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,25 @@ E.g. dedupe([5, 1, 1, 2, 3, 2, 5, 8]) target output: [5, 1, 2, 3, 8]
// Given an empty array
// When passed to the dedupe function
// Then it should return an empty array
test.todo("given an empty array, it returns an empty array");
test("given an empty array, it returns an empty array", () => {
const result = dedupe([]);
expect(result).toEqual([]);
});

// Given an array with no duplicates
// When passed to the dedupe function
// Then it should return a copy of the original array
test("given an array with no duplicates, returns a copy of the original array", () => {
const originalArray = ["a", "b", "c"];
const result = dedupe(originalArray);
expect(result).toEqual(originalArray);
});

// Given an array with strings or numbers
// When passed to the dedupe function
// Then it should remove the duplicate values
test("given an array with strings or numbers, removes the duplicate values", () => {
const arrayWithDuplicates = [1, "a", "b", 1, "c", "b", 2];
const result = dedupe(arrayWithDuplicates);
expect(result).toEqual([1, "a", "b", "c", 2]);
});