-
-
Notifications
You must be signed in to change notification settings - Fork 318
London | 26-ITP-May | Rizqah Popoola | Sprint 1 | Data Groups #1278
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
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,11 @@ | ||
| function dedupe() {} | ||
| function dedupe(input) { | ||
| const newArray = []; | ||
| if (input.length === 0) { | ||
| return []; | ||
| } | ||
| for (const i in input) | ||
|
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. Do you need two nested loops here? Could this be simplified? |
||
| for (const j in input) | ||
| if (i !== j && !newArray.includes(input[i])) newArray.push(input[i]); | ||
| return newArray; | ||
| } | ||
| module.exports = dedupe; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,14 @@ | ||
| function findMax(elements) { | ||
| if (elements.length === 0) { | ||
| return -Infinity; | ||
| } | ||
| const cleanElements = elements | ||
| .filter((item) => typeof item === "number") | ||
| .sort((a, b) => a - b); | ||
| if (cleanElements.length === 0) { | ||
| return -Infinity; | ||
| } else { | ||
| return cleanElements[cleanElements.length - 1]; | ||
| } | ||
| } | ||
|
|
||
| module.exports = findMax; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,17 @@ | ||
| function sum(elements) { | ||
| } | ||
| if (elements.length === 0) { | ||
| return 0; | ||
| } | ||
| const cleanElements = elements | ||
| .filter((item) => typeof item === "number") | ||
| .sort((a, b) => a - b); | ||
| let sum = 0; | ||
| if (cleanElements.length === 0) { | ||
|
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. Do you need to return if there are zero elements? |
||
| return 0; | ||
| } else { | ||
| for (let i = 0; i < cleanElements.length; i++) sum += cleanElements[i]; | ||
|
|
||
| return sum; | ||
| } | ||
| } | ||
| module.exports = sum; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,14 @@ | ||
| // Refactor the implementation of includes to use a for...of loop | ||
|
|
||
| function includes(list, target) { | ||
| for (let index = 0; index < list.length; index++) { | ||
| const element = list[index]; | ||
| for (const item of list) { | ||
| const element = item; | ||
|
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. What does the element constant do here? |
||
| if (element === target) { | ||
| return true; | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
| console.log(includes(["a", "b", "c", "d"], "c")); | ||
|
|
||
| module.exports = includes; | ||
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.
Well done adding test cases