Day 6: Numbers & Math Made Easy

Souvik SuralSouvik Sural
3 min read

๐Ÿ”ฃ Working with Numbers

const score = 400;
console.log(score); // 400
const balance = new Number(100);
console.log(balance);            // [Number: 100]
console.log(balance.toString()); // "100"
console.log(balance.toString().length); // 3
console.log(balance.toFixed(1)); // "100.0"
MethodDescriptionExampleOutput
.toString()Converts number to string100.toString()"100"
.toFixed(n)Formats to n decimals100.456.toFixed(2)"100.46"
.toPrecision(n)Total digits including before/after decimal123.8966.toPrecision(4)"123.9"
.toLocaleString()Adds comma separators1000000.toLocaleString('en-IN')"10,00,000"

๐Ÿงฎ Example:

const otherNumber = 123.8966;
console.log(otherNumber.toPrecision(4)); // "123.9"

const hundreds = 1000000;
console.log(hundreds.toLocaleString('en-IN')); // "10,00,000"

๐Ÿ”ข JavaScript Math Object

console.log(Math); // Full Math object

๐Ÿ”ง Common Math Methods

MethodDescriptionExampleOutput
Math.abs()Absolute valueMath.abs(-4)4
Math.round()Rounds to nearestMath.round(4.6)5
Math.ceil()Rounds upMath.ceil(4.2)5
Math.floor()Rounds downMath.floor(4.9)4
Math.min()Smallest valueMath.min(4, 3, 6, 8)3
Math.max()Largest valueMath.max(4, 3, 6, 8)8
Math.random()Random float (0 to <1)Math.random()0.321...
Math.pow(a, b)Power a^bMath.pow(2, 3)8
Math.sqrt()Square rootMath.sqrt(16)4
Math.trunc()Removes decimalMath.trunc(4.9)4
Math.sign()Returns sign (1/-1/0)Math.sign(-10)-1

๐ŸŽฒ Generating Random Numbers

console.log(Math.random());              // 0 to 0.999...
console.log((Math.random() * 10) + 1);   // 1 to 10.999...
console.log(Math.floor(Math.random() * 10) + 1); // 1 to 10

๐Ÿง  Random Number Between a Range (min to max)

const min = 10;
const max = 20;

console.log(Math.floor(Math.random() * (max - min + 1)) + min);

โœ… This gives you a number between 10 and 20 (inclusive).


โ“ Interview Q&A

  1. Whatโ€™s the difference between toFixed() and toPrecision()?
    โ†’ toFixed() fixes decimal places, toPrecision() controls total significant digits.

  2. How to round a number to the nearest integer in JavaScript?
    โ†’ Math.round(number)

  3. How to generate a number between 1 and 100 randomly?
    โ†’ Math.floor(Math.random() * 100) + 1

  4. Whatโ€™s the output of Math.floor(Math.random() * 10) + 1?
    โ†’ A random number between 1 and 10.

  5. Whatโ€™s the difference between Math.floor() and Math.trunc()?
    โ†’ floor() always rounds down, trunc() removes the decimal part (like parseInt()).


๐Ÿง  Homework Practice

  1. Try these and predict results:

     console.log((12345.6789).toLocaleString());
     console.log((12345.6789).toFixed(2));
     console.log((12345.6789).toPrecision(6));
    
  2. Generate:

    • Random number between 50 and 100

    • Round 3.14159 to 2 decimal places

    • Find max and min of [13, 7, 21, 4, 9]

  3. Bonus Challenge:

    • Build a function rollDice() that returns a number from 1 to 6.

๐Ÿ“Ž GitHub Repo

๐Ÿ”— Day 6 Code on GitHub

0
Subscribe to my newsletter

Read articles from Souvik Sural directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Souvik Sural
Souvik Sural

Campus Maven at RiseIn Postman Student Expert Web Developer and Blockchain enthusiast