-
-
Notifications
You must be signed in to change notification settings - Fork 153
London | May-2025 | Halimatou Saddiyaa | Module-Data-Groups | Sprint 1 #548
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 4 commits
a98fbbb
888e04b
71e477c
c91c934
f98230f
a434557
95f0256
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(array) { | ||
const seen = new Set(); | ||
return array.filter((item) => { | ||
if (seen.has(item)) { | ||
return false; | ||
} | ||
seen.add(item); | ||
return true; | ||
}); | ||
} | ||
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. Clever solution. Using a 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. Thanks, I don't need to use the filter function as I can directly use Set and populate it from an Array. I've corrected my code to reflect that. |
||
|
||
module.exports = dedupe; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,9 @@ | ||
function findMax(elements) { | ||
function findMax(arr) { | ||
const numbers = arr.filter((item) => typeof item === "number"); | ||
if (numbers.length === 0) { | ||
return -Infinity; | ||
} | ||
return Math.max(...numbers); | ||
} | ||
Comment on lines
+1
to
7
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. Excellent solution. 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. Thanks |
||
|
||
module.exports = findMax; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,10 @@ | ||
function sum(elements) { | ||
function sum(arr) { | ||
return arr.reduce((total, current) => { | ||
if (typeof current === "number") { | ||
return total + current; | ||
} | ||
return total; | ||
}, 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. This is good. However it would be more typical pattern to use 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. Thanks, I think using filter then reduce will be preferred because it separates the two steps and the code is then easier to read and understand. I've updated my code to reflect that. |
||
|
||
module.exports = sum; |
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 work. However there is an edge case that the tests don't require you to handle. You might want to look at the special
NaN
property and consider whether the code would work if this value appeared in one of the test arrays. As I say it is not required but might be something to watch out for in the future.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.
Thank you, the code might not work with Nan as although it is a number it's not a valid numeric value for math operations.
I've corrected my function to include the case of NaN and added a test to check.