-
-
Notifications
You must be signed in to change notification settings - Fork 321
London | 26-ITP-May | Vito Moratti | Sprint 1 | Module data groups/sprint1 #1297
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
cae4a14
f9211a3
183be23
8e59e19
f9080a3
72267a0
4f2f012
f48b3b9
4fe95d9
120a279
a02f092
a22313c
dc82a68
4522a37
c22b7f9
8f921db
0bfabe2
23d816f
9fc3bd1
ad1cb78
71b98b5
b58b9c2
6604d84
67e3d31
64b4a38
cba37a9
4312a67
0a9896d
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,12 @@ | ||
| function dedupe() {} | ||
| function dedupe(arr) { | ||
| let newArr = []; | ||
| for (let i = 0; i < arr.length; i++) { | ||
| if (!newArr.includes(arr[i])) { | ||
| newArr.push(arr[i]); | ||
| } | ||
| } | ||
| return newArr; | ||
| } | ||
|
|
||
| console.log(dedupe([1,2,2,2,4])) | ||
| module.exports = dedupe; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,13 +16,28 @@ 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, 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", () => { | ||
| expect(dedupe([1, 2, 3])).toEqual([1, 2, 3]); | ||
| }); | ||
|
Comment on lines
+26
to
+28
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 implementation is correct. However, this test could be improved to better ensure
This test should fail if the function returns the original array (instead of a copy of the original array). The current test checks only if both the original array and the returned array contain identical elements. Can you find out what this additional check is?
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. i have now added test to check if the returned array is a copy |
||
|
|
||
| // 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("given an array of strings or numbers, it returns a new array with no duplicates removed", () => { | ||
| 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]); | ||
| }); | ||
|
|
||
| const original = [1, 2, 3]; | ||
| const result = dedupe(original); | ||
|
|
||
| expect(result).toEqual([1, 2, 3]); | ||
| expect(result).not.toBe(original); | ||
|
Comment on lines
+39
to
+43
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. This test is correct, but shouldn't it be defined at line 27? |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,10 @@ | ||
| function findMax(elements) { | ||
|
|
||
| const filteredList = elements.filter( | ||
| (value) => typeof value === "number" && !Number.isNaN(value) | ||
| ); | ||
| return Math.max(...filteredList); | ||
|
|
||
| } | ||
|
|
||
| module.exports = findMax; | ||
| module.exports = findMax; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,13 @@ | ||
| function sum(elements) { | ||
| let counter = 0; | ||
|
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. Could also use |
||
| const filteredList = elements.filter((value) => typeof value === "number" && !isNaN(value) && isFinite(value)); | ||
|
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. Note: |
||
| for (let i = 0; i < filteredList.length; i++) { | ||
| counter += filteredList[i]; | ||
| } | ||
| return counter; | ||
| } | ||
|
|
||
| module.exports = sum; | ||
|
|
||
| console.log(sum([NaN, 1])); | ||
| console.log(sum([Infinity, -Infinity])); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do explore the difference between
.toSorted()and.sort().