Five utility functions for the park's financial and sensor systems have number-handling bugs. IEEE 754 floating point makes 0.1 + 0.2 !== 0.3, parseInt silently ignores trailing garbage, the global isNaN lies about strings, division by zero produces Infinity instead of an error, and .toFixed() returns a string that looks like a number but concatenates instead of adding.
These bugs are responsible for more production incidents than most people realise. You'll fix each one using the correct numeric tools.
- Run the tests — they will fail.
- Read each failure to understand the expected behaviour.
- Fix the bug in
starter/numbers.js. Each function has exactly one bug.
| Function | What it does | The bug |
|---|---|---|
totalPriceCents(prices) |
Sum an array of dollar prices and return the total in cents | Sums dollars first then converts — accumulates IEEE float errors. Should convert each price to cents first |
parseAge(input) |
Parse a string to an integer age, return null for invalid input |
parseInt("12abc") returns 12 — should reject strings that aren't purely numeric |
isStrictlyNaN(val) |
Return true only for NaN |
Uses global isNaN() which coerces — isNaN(undefined) and isNaN("hello") wrongly return true |
safeDivide(a, b) |
Divide a by b, return null if b is zero |
Returns Infinity or NaN on division by zero instead of null |
roundTo2(n) |
Round to 2 decimal places and return a number | .toFixed(2) returns a string — arithmetic on the result silently concatenates |
Open starter/numbers.js. Run starter/index.js to see the broken output:
node starter/index.jscd starter && pnpm install && pnpm test- For money: work in cents (integers) to avoid float precision issues.
Math.round(price * 100)converts each dollar amount to cents safely. Number(input)is stricter thanparseInt—Number("12abc")givesNaN.Number.isNaN(val)is the non-coercing version ofisNaN().- Check
b === 0before dividing. - Wrap
.toFixed(2)inNumber(...)or useparseFloat(...)to get back to a number.