-
-
Notifications
You must be signed in to change notification settings - Fork 278
Sheffield | 26-ITP-Jan | Mahammad Osman | Sprint 1 | Sprint-1 #1074
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
base: main
Are you sure you want to change the base?
Changes from all commits
90a5b84
16ca07c
b416084
f6f0f81
2c50cfb
d675989
de3abe7
5d875b2
bed5c4e
62a26b6
6378b4b
a234c5a
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,5 @@ | ||
| function dedupe() {} | ||
| function dedupe(list) { | ||
| return [...new Set(list)]; | ||
| } | ||
|
|
||
| module.exports = dedupe; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,18 +11,35 @@ E.g. dedupe([5, 1, 1, 2, 3, 2, 5, 8]) returns [5, 1, 2, 3, 8] | |
| E.g. dedupe([1, 2, 1]) returns [1, 2] | ||
| */ | ||
|
|
||
| // Acceptance Criteria: | ||
| describe("dedupe", () => { | ||
| // Acceptance Criteria: | ||
|
|
||
| // 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"); | ||
| // Given an empty array | ||
| // When passed to the dedupe function | ||
| // Then it should return an empty array | ||
| test("should return an empty array when passed 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 | ||
| // Given an array with no duplicates | ||
| // When passed to the dedupe function | ||
| // Then it should return a copy of the original array | ||
| test("should return a copy of the original array when there are no duplicates", () => { | ||
| const input = [1, 2, 3]; | ||
|
|
||
| // Given an array of strings or numbers | ||
| // When passed to the dedupe function | ||
| // Then it should return a new array with duplicates removed while preserving the | ||
| // first occurrence of each element from the original array. | ||
| const result = dedupe(input); | ||
|
|
||
| expect(result).toEqual([1, 2, 3]); | ||
| expect(result).not.toBe(input); | ||
| }); | ||
cjyuan marked this conversation as resolved.
Show resolved
Hide resolved
Comment on lines
+27
to
+34
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. There is a chance that, even though the returned value has incorrect elements (for example,
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. Done! Yes, it's because this test could still pass even if the original array were destroyed or mutated in the function. It's a very interesting trap. |
||
|
|
||
| // Given an array of strings or numbers | ||
| // When passed to the dedupe function | ||
| // Then it should return a new array with duplicates removed while preserving the | ||
| // first occurrence of each element from the original array. | ||
| test("should return a new array with duplicates removed while preserving the first occurrence of each element", () => { | ||
| expect(dedupe(["a", "a", "a", "b", "b", "c"])).toEqual(["a", "b", "c"]); | ||
| expect(dedupe([5, 1, 1, 2, 3, 2, 5, 8])).toEqual([5, 1, 2, 3, 8]); | ||
| expect(dedupe([1, 2, 1])).toEqual([1, 2]); | ||
| }); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,13 @@ | ||
| function findMax(elements) { | ||
| const numbersOnly = elements.filter( | ||
| (element) => typeof element === "number" && !isNaN(element) | ||
| ); | ||
|
|
||
| if (numbersOnly.length === 0) { | ||
| return -Infinity; | ||
| } | ||
|
|
||
| return Math.max(...numbersOnly); | ||
| } | ||
|
|
||
| module.exports = findMax; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,12 @@ | ||
| function sum(elements) { | ||
| const numbersOnly = elements.filter( | ||
| (element) => typeof element === "number" && !isNaN(element) | ||
| ); | ||
|
|
||
| if (numbersOnly.length === 0) { | ||
| return 0; | ||
| } | ||
| return numbersOnly.reduce((acc, curr) => acc + curr, 0); | ||
| } | ||
|
|
||
| module.exports = sum; |
Uh oh!
There was an error while loading. Please reload this page.