-
-
Notifications
You must be signed in to change notification settings - Fork 212
NW | 25-ITP-Sep| Ahmad Hmedan | Sprint 1 | Sprint 1 Coursework #833
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 12 commits
6e3bb1f
3dcaaab
cb84ef6
af680ac
94589a9
f16627a
36822dc
5aa44a8
cca85e9
e49a700
a3a1636
dc1f262
95c9737
339a932
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,17 @@ | ||
| function dedupe() {} | ||
| function dedupe(elements) { | ||
| const elementsClone = [...elements]; | ||
| if (elementsClone.length === 0) return elementsClone; | ||
|
|
||
| for (let i = 1; i < elementsClone.length; i++) { | ||
| for (let j = i; j > 0; j--) { | ||
| if (elementsClone[i] === elementsClone[j - 1]) { | ||
| elementsClone.splice(i, 1); | ||
| i--; // because I deleted one element from my array | ||
| break; | ||
| } | ||
| } | ||
| } | ||
|
||
| return elementsClone; | ||
| } | ||
|
|
||
| module.exports = dedupe; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,18 @@ | ||
| function findMax(elements) { | ||
| if (!Array.isArray(elements)) return -Infinity; | ||
| const elementsClone = [...elements]; | ||
|
|
||
| const numericElements = elementsClone.filter( | ||
| (item) => typeof item === "number" && !isNaN(item) | ||
| ); | ||
| if (numericElements.length === 0) return -Infinity; | ||
|
|
||
| let max = numericElements[0]; | ||
| for (let i = 0; i < numericElements.length; i++) { | ||
| if (max < numericElements[i]) max = numericElements[i]; | ||
| } | ||
|
|
||
| return max; | ||
| } | ||
|
|
||
| module.exports = findMax; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,12 @@ | ||
| function sum(elements) { | ||
| if (!Array.isArray(elements)) return 0; | ||
| const numericElements = elements.filter(Number.isFinite) | ||
| if (numericElements.length === 0) return 0; | ||
| let sum = 0; | ||
| for (let i = 0; i < numericElements.length; i++) { | ||
| sum += numericElements[i]; | ||
| } | ||
| return sum; | ||
| } | ||
|
|
||
| module.exports = sum; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,9 @@ | ||
| // 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]; | ||
| if (element === target) { | ||
| return true; | ||
| } | ||
| for (const element of list) { | ||
| if (element === target) return true; | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| 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.
There is a much more efficient syntax to access an element at a particular index without deleting any element from the array.