Glasgow | 26-ITP-May | Niangh Ciang | Sprint 1 | Exercises#1241
Glasgow | 26-ITP-May | Niangh Ciang | Sprint 1 | Exercises#1241Niangh-Ciang wants to merge 10 commits into
Conversation
LonMcGregor
left a comment
There was a problem hiding this comment.
Good work on these tasks. I have some questions in the comments to check you fully understand the solutions that you've written. Could you answer them please?
| if (!Array.isArray(list)) { | ||
| return null; | ||
| } | ||
| const numbersOnly = list.filter((value) => typeof value === "number"); |
There was a problem hiding this comment.
Do you know of any values which would pass this typeof check, but which are not actually numbers?
There was a problem hiding this comment.
I researched more about this, and I now understand that NaN, Infinity, and -Infinity would all pass the typeof value === "number" check, even though they are not valid finite numbers. These values are treated as “number” by JavaScript’s typeof operator, so they would still be included by my current filter.
There was a problem hiding this comment.
Good idea - is there an addition you can make to the filter that would catch these?
There was a problem hiding this comment.
I changed the filter to list.filter(Number.isFinite) so NaN, Infinity, and -Infinity don’t get included.
| return null; | ||
| } | ||
|
|
||
| const sortedList = [...numbersOnly].sort((a, b) => a - b); |
There was a problem hiding this comment.
What is the spread operator doing here?
There was a problem hiding this comment.
The spread operator makes a copy of the numbersOnly array before sorting it. Because .sort() changes the original array, using [...numbersOnly] lets me sort a copy instead of modifying the original.
There was a problem hiding this comment.
Does it matter if sort changes the original array? Why do you need to avoid that here?
There was a problem hiding this comment.
Since numbersOnly is already a new array created by filter(), sorting it doesn’t modify the original list. That means the spread operator isn’t strictly necessary for this function to work. Using numbersOnly.sort((a, b) => a - b) would also pass all the tests — including the one that checks the input array is not changed (expect(list).toEqual([3, 1, 2])). I used [...numbersOnly] simply to avoid unexpected side effects
| total += element; | ||
| } | ||
| } | ||
| total = Number(total.toFixed(2)); |
There was a problem hiding this comment.
I used toFixed(2) to avoid floating‑point precision errors. In my decimal test (expect(sum([-5, 2.3, 7.5, 3.9, 2.1])).toEqual(10.8)), JavaScript produces 10.799999999999 instead of 10.8. Rounding to 2 decimal places ensures the test passes correctly, which is why I included it.
There was a problem hiding this comment.
OK. This approach makes sense. Just be careful in future if you are working with arithmetic that you don't accidentally introduce inaccuracy by rounding more than you need to.
|
Good work - this task is done. Your reasoning for using [... array] makes sense, but I would suggest that if it's not strictly required you shouldn't use it as it adds a bit of unnecessary complexity to code. |
Self checklist
Changelist
Completed Sprint 1 exercises.