-
-
Notifications
You must be signed in to change notification settings - Fork 276
Cape Town | 26-ITP-Jan | Pretty Taruvinga | Sprint 1 | Data Groups #1104
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
53fa90f
86132f9
33ec99a
4bf6702
5db3aac
fc16e68
0e7b9da
d4e734a
459eb79
0a5f5cc
d55358c
5514777
eb1b8f2
d0ec9a3
90c1ff9
6aa82cf
afa2fdb
43cf84a
a548d71
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,7 @@ | ||
| function dedupe() {} | ||
| function dedupe(list) { | ||
| if (!Array.isArray(list)) { | ||
| return null; | ||
| } | ||
| return list.filter((value, index, self) => self.indexOf(value) === index); | ||
| } | ||
| module.exports = dedupe; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,13 +16,42 @@ E.g. dedupe([1, 2, 1]) returns [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, when passed to the dedupe function, then it should return an empty array", () => { | ||
| expect(dedupe([])).toEqual([]); | ||
| }); | ||
|
|
||
| // Given a non-array value | ||
| // When passed to the dedupe function | ||
| // Then it should return null | ||
| test("Given a non-array value, when passed to the dedupe function, then it should return null", () => { | ||
| expect(dedupe("not an array")).toBeNull(); | ||
| expect(dedupe(123)).toBeNull(); | ||
| expect(dedupe({})).toBeNull(); | ||
| }); | ||
|
|
||
| // 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, when passed to the dedupe function, then it should return a copy of the original array", () => { | ||
| const input = ["a", "b", "c"]; | ||
| const result = dedupe(input); | ||
|
|
||
| expect(result).toEqual(input); | ||
| expect(result).not.toBe(input); | ||
| expect(result.length).toBe(input.length); | ||
| }); | ||
|
Comment on lines
+35
to
+42
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
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. Thanks for pointing this out! You're right — the current test isn't strict enough. It only checks that result is equal to input and that it's a different reference, but it doesn't explicitly verify the contents or length of the array. This means a faulty implementation could potentially pass the test under certain conditions. To fix this, I strengthened the test by adding checks for the array length and ensuring the expected elements are present. This makes the test more robust and prevents false positives. Let me know if you'd like me to add more edge case tests as well! |
||
|
|
||
| // 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. | ||
| // Then it should remove the duplicate values, preserving the first occurrence of each element | ||
| test("Given an array with strings or numbers, when passed to the dedupe function, then it should remove the duplicate values, preserving the first occurrence of each element", () => { | ||
| expect(dedupe(["a", "a", "b", "c", "c"])).toEqual(["a", "b", "c"]); | ||
| expect(dedupe([1, 2, 2, 3, 4, 4])).toEqual([1, 2, 3, 4]); | ||
| }); | ||
|
|
||
| test("preserves the original order of first occurrences", () => { | ||
| const input = ["b", "a", "b", "c"]; | ||
| const result = dedupe(input); | ||
|
|
||
| expect(result).toEqual(["b", "a", "c"]); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,12 @@ | ||
| function findMax(elements) { | ||
| function max(numbers) { | ||
| let max = -Infinity; | ||
|
|
||
| for (let num of numbers) { | ||
| if (typeof num === "number" && num > max) { | ||
| max = num; | ||
| } | ||
| } | ||
| return max; | ||
| } | ||
|
|
||
| module.exports = findMax; | ||
| module.exports = max; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,14 @@ | ||
| function sum(elements) { | ||
| if (!Array.isArray(elements)) { | ||
| return null; | ||
| } | ||
| let total = 0; | ||
| for (let i = 0; i < elements.length; i++) { | ||
| if (typeof elements[i] === "number" && !isNaN(elements[i])) { | ||
| total += elements[i]; | ||
| } | ||
| } | ||
| return total; | ||
| } | ||
|
|
||
| module.exports = sum; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.