-
-
Notifications
You must be signed in to change notification settings - Fork 210
London | ITP-MAY-25 | Surafel Workneh| Module: Data Groups | Sprint-1 #681
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
Conversation
…If no valid numbers, return null
…urn the middle plus
…t returns a copy of the original array
…ical element of an array
…numerical elements of an array
…ical elements of an array
a-robson
left a comment
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.
Excellent. Code quality is high and you are making great progress. I've made some comments and suggestions.
| // Clone and sort the array numerically | ||
| const sorted = [...nums].sort((a, b) => a - b); |
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.
Overall this is excellent. But is it necessary to clone the array here? Could we just sort it?
| function dedupe(arr) { | ||
| if (!Array.isArray(arr)) return []; | ||
|
|
||
| const seen = new Set(); | ||
| const result = []; | ||
|
|
||
| for (const item of arr) { | ||
| if (!seen.has(item)) { | ||
| seen.add(item); | ||
| result.push(item); | ||
| } | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
|
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.
Very good. Using a Set is the right approach. However there are easier ways in Javascript to populate a Set from an array. Do we need a for loop at all?
| function sum(elements) { | ||
| if (!Array.isArray(elements)) return 0; | ||
|
|
||
| return elements.reduce((acc, val) => { | ||
| return typeof val === "number" && !isNaN(val) ? acc + val : acc; | ||
| }, 0); | ||
| } |
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.
Very good. However using filter() then reduce() is a more common approach to this type of problem. What are the advantages of using this pattern rather than having the 'filtering logic' within the reduce() function?
Learners, PR Template
Self checklist
Changelist
Briefly explain your PR.
Questions
Ask any questions you have for your reviewer.