-
-
Notifications
You must be signed in to change notification settings - Fork 310
Glasgow | 26-ITP-May | Niangh Ciang | Sprint 1 | Exercises #1241
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 all commits
a753854
3cbe8a9
f22f510
7b69ec9
2e90135
c03e930
650ead1
7fd5ba8
5467d65
1fbbc08
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,5 @@ | ||
| function dedupe() {} | ||
| function dedupe(array) { | ||
| if (!Array.isArray(array)) return []; | ||
| return [...new Set(array)]; | ||
| } | ||
| module.exports = dedupe; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,16 @@ | ||
| function findMax(elements) { | ||
| if (!Array.isArray(elements)) return -Infinity; | ||
| let max = -Infinity; | ||
|
|
||
| for (const element of elements) { | ||
| if (typeof element === "number") { | ||
| if (element > max) { | ||
| max = element; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return max; | ||
| } | ||
|
|
||
| module.exports = findMax; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,15 @@ | ||
| function sum(elements) { | ||
| if (!Array.isArray(elements)) return 0; | ||
|
|
||
| let total = 0; | ||
|
|
||
| for (const element of elements) { | ||
| if (typeof element === "number") { | ||
| total += element; | ||
| } | ||
| } | ||
| total = Number(total.toFixed(2)); | ||
|
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. Why are you using toFixed here?
Author
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. 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. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 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. |
||
| return total; | ||
| } | ||
|
|
||
| module.exports = sum; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| const fs = require("fs"); | ||
|
|
||
| const raw = fs.readFileSync("input.txt", "utf8"); | ||
|
|
||
| const lines = raw.split("\n"); | ||
| const numbers = lines.map(Number); | ||
| function findFinalFrequency(nums) { | ||
| let total = 0; | ||
|
|
||
| for (const n of nums) { | ||
| total += n; | ||
| } | ||
|
|
||
| return total; | ||
| } | ||
| console.log(findFinalFrequency(numbers)); |
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.
What is the spread operator doing here?
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.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does it matter if sort changes the original array? Why do you need to avoid that here?
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.
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