London | 26-ITP-JAN | Xuanming Hu | Sprint 1 | Data Groups#1007
London | 26-ITP-JAN | Xuanming Hu | Sprint 1 | Data Groups#1007Samual-Hu wants to merge 2 commits intoCodeYourFuture:mainfrom
Conversation
Sprint-1/implement/max.js
Outdated
| const numericElements = elements.filter(item => typeof item === "number"); | ||
|
|
||
| if (numericElements.length === 0) { | ||
| return -Infinity; | ||
| } | ||
|
|
||
| return Math.max(...numericElements); |
There was a problem hiding this comment.
What do you expect from the following function calls (on extreme cases)?
Does your function return the value you expected?
findMax([NaN])
findMax([0, NaN, 1])
There was a problem hiding this comment.
For findMax([NaN]), I expected -Infinity, but the actual result is NaN.
For findMax([0, NaN, 1]), I expected 1, but the actual result is also NaN.
This is because the type of NaN is number.
There was a problem hiding this comment.
If the function does not behave the way you expected, why not update the function so that it matches your expectation?
| let total = 0; | ||
|
|
||
| for (let i = 0; i < elements.length; i++) { | ||
| if (typeof elements[i] === "number") { | ||
| total += elements[i]; | ||
| } | ||
| } | ||
|
|
||
| return total; |
There was a problem hiding this comment.
What do you expect from the following function calls (on extreme cases)?
Does your function return the value you expected?
sum([NaN, 1]);
sum([Infinity, -Infinity]);
There was a problem hiding this comment.
For sum([NaN, 1]), I expected 1, but the actual result is NaN.
For sum([Infinity, -Infinity]), I expected 0, but the actual result is NaN.
This is also because the type of NaN is number.
|
Hi @cjyuan , Thank you for your comments. I have resolved the indentation issues and conducted additional tests. |
|
Thanks for the explanations. Updating the check in So all good. |
Learners, PR Template
Self checklist
Changelist
Completed all exercises in the Sprint 1 directory.
Questions
N/A