Strings & numeric operations
Understand the idea
A display rounds a price correctly, but the next addition joins digits as text. Formatting changed the type of the value.
One boundary between values and display
A price needs arithmetic before it needs a currency label. Mixing those steps can turn a later addition into string joining. Separating calculation from display also makes it easier to test the number independently of how it looks on screen.
Strings produce new strings
Strings are sequences. such as trim, slice, and toUpperCase return strings without changing the original. Indexes describe positions, and slice's end position is excluded. For user-facing text, remember that a visible symbol can occupy more than one JavaScript string code unit.
A string is text in quotes. A template literal uses backticks and can insert the result of an between a dollar sign and braces. String methods such as trim return a new string; they do not edit the original text in place.
| Operation | Returns | Useful for |
|---|---|---|
| String.trim() | A new string | Removing whitespace at the ends |
| String.includes(text) | A boolean | Checking whether text occurs |
| number.toFixed(2) | A string | Displaying a chosen number of decimal places |
| Math.round / floor / ceil | A number | Nearest integer / down / up |
| Math.random() | A number from 0 inclusive to 1 exclusive | Casual randomness, not security |
| Intl.NumberFormat | Locale-aware display text | Formatting a number for the reader’s locale |
Numbers have precision limits
Template literals insert expression results into text. They are useful for a final label, but that label is not suitable for arithmetic. Keep the underlying numeric value separately instead of trying to recover it from a currency label later.
Rounding and randomness
Math.floor rounds downward, Math.ceil upward, and Math.round to a nearest integer according to its tie rule. toFixed produces a formatted string. JavaScript numbers use binary floating point, so some decimal fractions cannot be represented exactly. For ordinary currency calculations, integer minor units help avoid accumulating those rounding surprises.
Build the final label
Math.random produces a value from zero inclusive to one exclusive. Scaling and flooring can select an index; rounding can bias the endpoints. It is useful for a learning game, but not for security-sensitive tokens.
The example stores 250 cents instead of starting with the decimal 2.50. It calculates the total, converts cents to the display unit, and formats two decimal places at the end. Integers are exact within the safe-integer range; fractional rates and rounding policies still need deliberate handling in a real money calculation.
Mental model
FOLLOW THE VALUE
Calculate before formatting.
01 / 03Trace the transformation
Live example
The total remains a number in cents. Only the final label formats it as dollars with two decimal places.
Your console output appears here.
Guided challenge
Normalize the display name
Remove surrounding whitespace before building the label. Keep all arithmetic in numeric cents.
Your console output appears here.
EXPECTED CONSOLE OUTPUT
Ada: $7.50 number
Hints 0/2
Use all hints or make 3 failed runs (0/3).
Free sandbox
Make a variation, test an assumption, or break the example on purpose. This draft is saved separately.
Your console output appears here.
Quiz
A quick check for understanding. Retry as often as you like; your learning path stays open.
Ready to call this one understood?
Mark this lesson complete to earn 100 XP. You can always revisit it.