Skip to content

Exercise 3 of 10 · Arithmetic

Party Planner

What you will make

A pizza order that works itself out from the guest list: the slices wanted, the whole pizzas that covers, and the slices that spill past them.

The one new idea: JavaScript does arithmetic, and whole-number division needs Math.floor and %

Splitting a count into whole groups and a remainder turns up everywhere — pizzas, pages, boxes, minutes into hours. JavaScript hands back a decimal until you ask for something else, so knowing which tool asks the whole-number question is what stops a program quietly ordering 5.25 pizzas.

Go straight to the code ↓

Arithmetic, and the division that never rounds

JavaScript does sums. You write them much as you would on paper, with one symbol swapped out:

  • + adds and - subtracts, exactly as they appear
  • * multiplies, using a star rather than an x
  • / divides, using a slash

So guests * slicesEach means the guests multiplied by the slices each, and JavaScript works the answer out the moment it reaches that line. Nothing is rounded behind your back — and that is where JavaScript may surprise you.

JavaScript has one kind of number. Not a whole one and a separate decimal one: just a number. So / always answers the share-it-out-exactly question, decimal point and all. 16 / 8 gives 2, because two is the exact answer. 17 / 8 gives 2.125. If you have met a language where dividing two whole numbers hands back a whole number, this is not that language, and there is no second slash here that makes it one.

A pizza question needs whole numbers, because nobody delivers 5.25 pizzas. So you ask two narrower questions, and JavaScript has a tool for each.

How many whole ones fit? Math.floor(...) throws away everything after the decimal point and leaves the whole number below it, so Math.floor(17 / 8) is 2. Read that name the way you read console.log: a capital M, a dot, then what you want done, with the brackets holding what you hand over. The capital letter is not optional.

What is left over? % answers that by itself. 17 % 8 is 1 — the slice those two whole pizzas do not cover. You may know % as the percent sign; here it means remainder, the part that did not fit.

A worked example

Cupcakes rather than pizzas, so the party in the editor stays yours to finish:

JavaScript
const cupcakes = 26;
const perBox = 4;

console.log("Plain divide ..", cupcakes / perBox);
console.log("Full boxes ....", Math.floor(cupcakes / perBox));
console.log("Left over .....", cupcakes % perBox);
Output
Plain divide .. 6.5
Full boxes .... 6
Left over ..... 2

Three lines, the same two numbers, three different and equally true answers. 6.5 is the honest share, six is how many boxes you can fill, and two is what is still on the tray. The rows of dots are only text inside the quotes, and the comma inside console.log sets the label and the number down on one line with a single space between them.

Your turn

The editor has a party to plan. Fourteen guests are coming, each one will eat three slices, and a pizza turns up cut into eight.

The program already multiplies the guests by the slices to get the total wanted, and it already prints the summary block. Two lines are unfinished:

JavaScript
const pizzas = 0;   // whole pizzas - change this
const extra = 0;    // slices past those - and this

Replace each 0 with a sum built from slicesNeeded and perPizza. Give pizzas the number of whole pizzas the order fills, and extra the slices that spill past them. Both lines keep their const: the name is settled the once, and writing the sum where the 0 sits is settling it properly rather than changing it later. Notice the names while you are there — JavaScript joins words with a capital letter rather than an underscore, which is why it is slicesEach and never slices_each.

Then run it and read the block back. If the last two numbers still show 0, nothing has changed yet — the program is perfectly happy with zeros in it, it simply plans a party with no food. When it works, five whole pizzas cover forty slices and two slices are still wanted on top.

If something goes wrong

The likeliest slip announces itself as a dot in the middle of a number — Whole pizzas ... 5.25. The division is there and it is right; Math.floor is simply not wrapped around it yet, so you are being told the exact share rather than a count of boxes you could carry.

Two others are worth recognising. ReferenceError: math is not defined means the capital M went missing, and Math is the only spelling JavaScript knows. A Whole pizzas line reading 42 means two slashes where one was meant: two of them begin a comment, so the rest of that line is ignored and no error appears at all.

Nothing here can break. Change a number, run it again, and read your own summary.

Write your code

Runs in your browser. Press Run (or Ctrl/Cmd+Enter) and the output is checked for you.

Ctrl/Cmd+Enter to run

Press Esc then Tab to move keyboard focus out of the code editor.

Ready
Output will appear here after you run your code.

The runtime is starting in the background. You can type now — it will be ready before you are.

The answer appears here once you have run your code at least once.

Things that often go wrong here

Dividing with a single slash and stopping there
The summary then claims 5.25 whole pizzas. That is a correct answer to a division sum and a useless one to a pizza question, because nobody delivers a quarter of a pizza. Division in JavaScript never rounds for you.
Using two slashes for whole-number division
Some other languages split into whole groups that way. In JavaScript two slashes start a comment, so the rest of that line is ignored: pizzas is left holding the full slice count of 42 and no error appears at all, because a line ending in a comment is a perfectly normal thing to write.
Writing math.floor with a small m
Capital letters matter in JavaScript. Math is a built-in name that begins with a capital M, so the small version sends JavaScript looking for something that does not exist and it stops with ReferenceError: math is not defined.
Putting the two sums the wrong way round
Nothing breaks and the block prints as neatly as ever, but the numbers stop hanging together: it reads 2 whole pizzas and 5 extra slices, which covers 21 slices rather than the 42 the same block asked for two lines earlier.

Want a blank editor instead? Open the JavaScript playground.