-
-
Notifications
You must be signed in to change notification settings - Fork 283
London | 26-ITP-January | Eugenie Ahangama | Sprint 1 | Data Groups #998
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
e1abbb2
ed6b096
afb1a3f
4779fd1
71dc25e
6bf36a6
68cee2e
7866c4b
f9f7dc0
31bcc22
f786fca
fb96503
7dcc6d3
faa0f40
457ca65
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,9 @@ | ||
| function dedupe() {} | ||
| function dedupe(arr) { | ||
| // Return null if the input is not an array | ||
| if (!Array.isArray(arr)) return null; | ||
| // Use Set to remove duplicates, | ||
| // then spread back into a new array | ||
| return [...new Set(arr)]; | ||
| } | ||
|
|
||
| module.exports = dedupe; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,12 +16,27 @@ E.g. dedupe([1, 2, 1]) target output: [1, 2] | |
| // 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", () => { | ||
| expect(dedupe([])).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, it returns a copy of the original array", () => { | ||
| const input = [1, 2, 3]; | ||
| const result = dedupe(input); | ||
| // Check the contents match a hardcoded expected value, not the input itself | ||
| expect(result).toEqual([1, 2, 3]); | ||
| // Check it's a different array, not the same reference | ||
| expect(result).not.toBe(input); | ||
| }); | ||
|
Comment on lines
+26
to
33
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Your function is correct, but currently there is a chance that an incorrectly implemented function can pass this test. Can you figure out why?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A function that just returns a copy without deduplicating (e.g. I've added an extra check using an array with duplicates |
||
|
|
||
| // Given an array with strings or numbers | ||
| // When passed to the dedupe function | ||
| // Then it should remove the duplicate values, preserving the first occurence of each element | ||
| test("given an array with duplicate numbers or strings, it removes duplicates preserving first occurence", () => { | ||
| expect(dedupe([5, 1, 1, 2, 3, 2, 5, 8])).toEqual([5, 1, 2, 3, 8]); | ||
| expect(dedupe(["a", "a", "a", "b", "b", "c"])).toEqual(["a", "b", "c"]); | ||
| expect(dedupe([1, 2, 1])).toEqual([1, 2]); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,8 @@ | ||
| function findMax(elements) { | ||
| // Filter to only numeric values | ||
| const nums = elements.filter((x) => typeof x === "number" && isFinite(x)); | ||
| // Math.max with no arguments returns -Infinity, which handles empty arrays as well | ||
| return Math.max(...nums); | ||
| } | ||
|
|
||
| module.exports = findMax; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,9 @@ | ||
| function sum(elements) { | ||
| // Filter to only numeric values, | ||
| // then reduce to a total | ||
| return elements | ||
| .filter((x) => typeof x === "number" && isFinite(x)) | ||
| .reduce((total, x) => total + x, 0); | ||
| } | ||
|
|
||
| module.exports = sum; |
Uh oh!
There was an error while loading. Please reload this page.