In this video, you followed one bad weight through a pack script and saw how far its NaN travels.
Key Points
NaNspreads: AddingNaNto other numbers givesNaN, so one bad conversion turns the whole total intoNaN, and the good weights get lost with it.- Comparisons with
NaNanswerfalse: Asking whetherNaNis bigger, smaller or equal always givesfalse. AfalselikeLight enough: falsecan look like a real answer when the script never knew the weight. NaNisn’t equal to itself:Number("1.5kg") === NaNisfalse, becauseNaNisn’t equal to anything, not even anotherNaN. Check withNumber.isNaN()instead, which givestrueforNaN.- Trace a strange number back: When a printed number looks wrong, follow it up the script, line by line, to the first line that made it.
- Decimals can be a tiny bit off:
0.1 + 0.2gives0.30000000000000004, because most decimals can’t be stored exactly. For now, round only when you show a number. For money, count in whole cents, because small whole-cent amounts add up exactly. - Dividing by zero doesn’t stop your program:
6 / 0givesInfinity, a number bigger than every finite number.
The Script
The pack script from the video, with the sleeping bag’s unit still stuck on: