Home › Learn › JavaScript › Getting started

Numbers and math

Lesson 3 of 40. Arithmetic, remainders, powers, and the Math object.

The arithmetic operators are +, -, *, /, % (remainder), and ** (power). Division gives decimals: 7 / 2 is 3.5. To split into whole shares, use Math.floor(7 / 2). Math.round, Math.ceil, Math.abs, Math.max, Math.min, Math.sqrt, and Math.PI live on the Math object.

Numbers are binary floating point, so 0.1 + 0.2 is 0.30000000000000004. Round before showing money: Math.round(x * 100) / 100 keeps two decimals as a number, and x.toFixed(2) gives a string.

console.log(17 % 5);            // 2
console.log(2 ** 10);           // 1024
console.log(Math.floor(7 / 2)); // 3
console.log(Math.round(2.5));   // 3

Your task

The starter defines apples, people, and radius. Compute each, the whole number of apples each person gets; left, the apples left over; and area, the area of a circle with that radius, Math.PI * radius ** 2, rounded to two decimals with Math.round.

Hint 1

Math.floor(apples / people) and apples % people.

Hint 2

Math.round(Math.PI * radius ** 2 * 100) / 100

Show the solution
const apples = 17;
const people = 5;
const radius = 4;

const each = Math.floor(apples / people);
const left = apples % people;
const area = Math.round(Math.PI * radius ** 2 * 100) / 100;

    Runs in an isolated web worker in this tab; nothing is sent anywhere.