Byte becomes an accountant
Arithmetic, %, rounding, toFixed, Number and parseInt, NaN, Infinity and random numbers.
Let's start easy. Five basic operations:
+and-— addition and subtraction;*— multiplication;/— division, and the result can be a fraction:7 / 2is3.5, not3;**— power:2 ** 10is1024.
The order is the one from school: ** first, then * and /, then + and -. When in doubt, add parentheses.
const herring = 120;
const salmon = 450;
console.log(herring + salmon);
console.log(salmon - herring);
console.log(herring * 3);
console.log(salmon / 4);
console.log(2 + 3 * 4);
console.log((2 + 3) * 4);Now an accountant's favourite operator: %, the remainder. A crate holds 6 fish and we have 20. How many full crates? Math.floor(20 / 6) — 3. How many fish are left without a crate? 20 % 6 — 2.
The remainder shows up more often than you'd think: n % 2 === 0 means the number is even, minutes % 60 gives the minutes past the full hours, i % 3 === 0 picks every third customer.
const fish = 20;
const perCrate = 6;
console.log(Math.floor(fish / perCrate)); // full crates
console.log(fish % perCrate); // fish without a crate
console.log(135 % 60); // minutes past two hours
console.log(10 % 2 === 0); // is 10 even?Rounding means four functions on the Math object, each with its own personality:
Math.round(x)— to the nearest integer;Math.floor(x)— the "floor": always down;Math.ceil(x)— the "ceiling": always up;Math.trunc(x)— simply chops off the fractional part.
For positive numbers floor and trunc agree. For negative ones they don't: "down" from -47.6 is -48.
const weight = 47.6;
console.log(Math.round(weight));
console.log(Math.floor(weight));
console.log(Math.ceil(weight));
console.log(Math.trunc(weight));
console.log(Math.floor(-47.6));
console.log(Math.trunc(-47.6));const total = 349.9;
console.log(total.toFixed(2));
console.log(typeof total.toFixed(2));
console.log(total.toFixed(2) + 1);
console.log(Number(total.toFixed(2)) + 1);350.9 but "349.901" — that's how weird receipts are born.Prices arrive from a form as strings: "42". You can't do maths with a string, so first you turn it into a number:
Number(s)— strict: the whole string must be a number, otherwiseNaN;parseInt(s, 10)— reads an integer from the start of the string and silently throws the rest away;parseFloat(s)— the same, but it understands a fractional part.
NaN stands for "Not a Number". It's JavaScript's way of saying "I tried, but this is nonsense".
console.log(Number("42"));
console.log(Number("42 dollars"));
console.log(parseInt("42 dollars", 10));
console.log(parseFloat("3.5 lb"));
console.log(Number(""));0.const price = Number("lots");
console.log(price);
console.log(typeof price);
console.log(price === NaN);
console.log(Number.isNaN(price));
console.log(price + 100);"number".Two more special numbers: Infinity and -Infinity. Dividing by zero gives you one: JavaScript doesn't crash, it honestly says "infinity".
And a few helpers from Math: Math.max(...) and Math.min(...) pick the largest and smallest of any number of arguments, and Math.abs(x) drops the sign — handy when the size of a difference matters, not its direction.
console.log(450 / 0);
console.log(-450 / 0);
console.log(Math.max(120, 450, 90));
console.log(Math.min(120, 450, 90));
console.log(Math.abs(300 - 450));Math.abs keeps the minus sign from confusing the report.Finally, the "Fish of the Day" raffle. Math.random() returns a random fraction from 0 (inclusive) to 1 (exclusive). To get an integer from min to max:
- multiply by the number of options,
max - min + 1— that gives a fraction from0to almost6; - Math.floor turns it into an integer from
0to5; - add
minto shift the range to1…6.
function randomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
console.log(randomInt(1, 6));
console.log(randomInt(1, 6));
console.log(randomInt(100, 200));0.1 + 0.2. JavaScript said 0.30000000000000004. I recounted three times on my paws. Got 0.3. Which of us is lying? We'll find out next lesson. Meanwhile, a tip: I now count money in cents, as whole numbers.console.log(0.1 + 0.2);
console.log(10 + 20);Pop quiz
Mistakes are fine: keep trying until you find the right answer, and Byte explains why it's right.