-
-
Notifications
You must be signed in to change notification settings - Fork 286
West Midlands | 26-Jan-ITP | Fida Ali Zada | Sprint 1 | Data Groups #982
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 6 commits
d5f3e06
868c5b9
08758cb
0c5f55e
f978ff5
182d369
b612d99
7c10e76
dacc81f
b62ff40
f51817c
e9ae929
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,13 @@ | ||
| function dedupe() {} | ||
| function dedupe(array) { | ||
| let arr = array.filter((item, index) => { | ||
|
|
||
| // indexOf() returns the first index where that value appears in the array. | ||
| if (array.indexOf(item) === index) { | ||
| return item; | ||
| } | ||
| }) | ||
|
cjyuan marked this conversation as resolved.
Outdated
|
||
|
|
||
| return arr; | ||
| } | ||
|
|
||
| module.exports = dedupe; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,12 +16,23 @@ 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("return an empty array for 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("if no duplicates, return the copy of the original array", () => { | ||
| expect(dedupe([1, 3, 5, 7])).toEqual([1, 3, 5, 7]); | ||
| }) | ||
|
|
||
|
Comment on lines
24
to
+35
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 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. By implementing
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. The concern here is not about your function implementation -- it was correct. The issue is, how to prepare a test to check if the function is indeed returning a copy of the original array. With the test you had, a function like this could also pass the test. function dedupe(array) {
const set = new Set(array);
if (set.size == array.length) return array;
return [...set];
}
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.
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. With this test, 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. Is it alright, this time?
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. Still not correct. Try looking up the difference between |
||
| // 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("an array with strings strings or numbers", () => { | ||
| expect(dedupe([2, 2, 3, 3, 3, "Black", "Black", "Black", "Green"])).toEqual([2, 3, "Black", "Green"]); | ||
| }) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,12 @@ | ||
| function findMax(elements) { | ||
| const filteredArr = elements.filter(x => typeof x === 'number'); | ||
|
cjyuan marked this conversation as resolved.
Outdated
|
||
|
|
||
| if (filteredArr.length == 0) { | ||
| return -Infinity; | ||
| } | ||
|
|
||
| // using 'spread sytax' | ||
| return Math.max(...filteredArr); | ||
| } | ||
|
|
||
| module.exports = findMax; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,9 @@ | ||
| function sum(elements) { | ||
|
|
||
| // const filteredArr = elements.filter(x => typeof x === 'number'); | ||
| // return filteredArr.reduce((a, b) => a + b, 0); | ||
|
|
||
| return elements.filter(x => typeof x === 'number').reduce((a, b) => a + b, 0); | ||
| } | ||
|
|
||
| module.exports = sum; |
|
cjyuan marked this conversation as resolved.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| function sum(list) { | ||
| return list.reduce((a, b) => a + b, 0); | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.