thecodezone.
The Code Zone
Short steps, at your pace · Free lessons and practice · Member video walkthroughs
Save your next small win.

Checking your account…

002 · Values and comparisons / Practice · 20 min

Calculate, format and inspect numbers

0.1 + 0.2 displays 0.30000000000000004 because binary floating-point cannot represent those decimal fractions exactly. Number.MAX_SAFE_INTEGER is 2 ** 53 - 1. The value 2 ** 53 is representable, but adding 1 no longer produces a distinct integer.

Activity 2 of 5 in Values and comparisons

Cartoon Rob demonstrates the working with numbers challenge with its actual inputs and outcomes.
Rob’s comic strip · Values and comparisons

Your challenge

Keep num1 = 8, num2 = 4 and pi = 3.14159 initially. Print addition, multiplication, division, num1 squared and (num1 + 3) * (num2 - 1) / 2. Then print 0.1 + 0.2, 2 ** 53, 2 ** 53 + 1 and Infinity - Infinity. Finally print the type of pi, pi fixed to two decimal places, pi converted to a string, that string’s type, its conversion back to a number and its type, round/floor/ceil of pi, then Math.random(). Keep this exact output order; the checks supply repeatable random values.

Keep the supplied input variables at the top. The checks change only those initial values in a separate sandbox and compare the result. They also check the explicitly requested code structures. Your own editor and saved draft are not changed by the scenario checks.

Numbers have precision limits

0.1 + 0.2 displays 0.30000000000000004 because binary floating-point cannot represent those decimal fractions exactly. Number.MAX_SAFE_INTEGER is 2 ** 53 - 1. The value 2 ** 53 is representable, but adding 1 no longer produces a distinct integer.

Formatting is different from calculating

toFixed(2) returns text. Number(text) converts numeric text back to a number. Math.round, floor and ceil return numbers. The original complex expression with 8 and 4 evaluates to 16.5, not 22. Math.random() returns a value at least 0 and strictly less than 1; its test values are supplied, not hardcoded in your solution.

Run, check and explain

Press Run code to see your current output. Choose Check challenge for named feedback on the starting case and variations. After three failed checks—or immediately after passing—you can reveal an explained example without replacing your code.

Signed-out work stays on this browser. Signed-in learners can save an account draft, submit for review and share a separate help snapshot. A passed browser check is practice feedback, not an instructor-certified grade.

YOUR TURN

Working with numbers

Keep num1 = 8, num2 = 4 and pi = 3.14159 initially. Print addition, multiplication, division, num1 squared and (num1 + 3) * (num2 - 1) / 2. Then print 0.1 + 0.2, 2 ** 53, 2 ** 53 + 1 and Infinity - Infinity. Finally print the type of pi, pi fixed to two decimal places, pi converted to a string, that string’s type, its conversion back to a number and its type, round/floor/ceil of pi, then Math.random(). Keep this exact output order; the checks supply repeatable random values.

To pass this challenge

Checks run separate copies with supplied inputs; your editor stays unchanged.

  • Arithmetic, precision, conversion and rounding
  • Calculations follow changed input values
Sample output for the starting inputs
12
32
2
64
16.5
0.30000000000000004
9007199254740992
9007199254740992
NaN
number
3.14
3.14159
string
3.14159
number
3
3
4
0.25

Random values are supplied during checks. Run code uses real randomness.

Run code displays your current output. Check challenge checks the named scenarios and requested structures in a separate sandbox. Save account draft keeps your code across devices when signed in. Word wrap is enabled.

Ready to checkRun the checks to see your results here.

Need a nudge?

Keep the requested sequence

Do the five arithmetic lines first, then the four precision experiments, then the ten pi/random lines.

Check types explicitly

Log typeof piString and typeof piNumber so you can distinguish formatted text from a numeric value.

Replace your editor code?

Your current work will be replaced. Choose Cancel to keep it.

ONE STEP FORWARD

Pass the checks, change one input yourself and explain why the output changes.