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

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.
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.
Your check results
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.
One possible solution
Your code may look different and still be correct. Compare the approach with your own.
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. 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.
const num1 = 8;
const num2 = 4;
const pi = 3.14159;
console.log(num1 + num2);
console.log(num1 * num2);
console.log(num1 / num2);
console.log(num1 ** 2);
console.log((num1 + 3) * (num2 - 1) / 2);
console.log(0.1 + 0.2);
console.log(2 ** 53);
console.log(2 ** 53 + 1);
console.log(Infinity - Infinity);
console.log(typeof pi);
console.log(pi.toFixed(2));
const piString = pi.toString();
console.log(piString);
console.log(typeof piString);
const piNumber = Number(piString);
console.log(piNumber);
console.log(typeof piNumber);
console.log(Math.round(pi));
console.log(Math.floor(pi));
console.log(Math.ceil(pi));
console.log(Math.random());
7 days freeTry The Code Zone
THE CODE ZONE COMMUNITY
Your next step is easier together.
Try the community free for 7 days.
No charge during your trial. Choose your plan on Skool.
- Ask questions and get support in the community
- Watch every course video walkthrough
- Find a study buddy in the 24/7 study room
Make the most of your trial with Premium.
Choose Premium on Skool to try Thursday live help sessions. Bring your questions, work through code, and get unstuck together.
Scheduled Thursdays in Eastern Time. Check the Skool calendar for your local date and time.
Have a website account? Sign in