Exercise 3 of 10 · Arithmetic
Egg Boxes
What you will make
A farm report that works out its own egg count from the hens, then the full boxes that count fills and the eggs left over.
The one new idea: numbers do arithmetic, and Math.floor and % split a count into whole groups and a remainder
Splitting an amount into whole groups and a remainder turns up everywhere: boxes, pages, teams, minutes into hours. Division in TypeScript keeps the decimals, so knowing how to ask the whole-number question is what stops a report promising 7.5 boxes.
Go straight to the code ↓Sums the way a keyboard writes them
TypeScript works sums out much as you would on paper, with a couple of symbols swapped for ones a keyboard has:
+adds and-subtracts*multiplies, a star in place of a times sign/divides- round brackets group part of a sum so it is worked out first
console.log(9 - 1 * 5);
console.log((9 - 1) * 5);
console.log(45 / 6);4
40
7.5Multiplying happens before subtracting unless brackets say otherwise, as in school maths. The third line is the
one to look at. TypeScript's everyday type for numbers is called number, and it holds whole numbers and decimals
alike. Nothing about a number says it has to be whole, so / keeps the decimals: 45 eggs split into sixes is 7.5.
A report about egg boxes needs two narrower answers instead.
How many whole groups fit? Math.floor(...) takes a number and rounds it down to a whole number,
so for a count like this one it drops everything after the decimal point. Math.floor(45 / 6) is 7. It is written with a capital M, a dot, then
floor, with the brackets holding what you hand over.
What is left over? % gives the remainder of a division by itself: 45 % 6 is 3, the eggs that seven
full boxes of six do not use. On a keyboard it is the percent sign; in a sum it means remainder.
A worked example
Pencils and packs rather than eggs and boxes, so the report in the editor stays yours to finish:
const pencils = 50;
const perPack = 12;
console.log("Exact share ...", pencils / perPack);
console.log("Full packs ....", Math.floor(pencils / perPack));
console.log("Spare pencils .", pencils % perPack);Exact share ... 4.166666666666667
Full packs .... 4
Spare pencils . 2Three answers from the same two numbers. The long decimal is the share with its decimals kept, 4 is how many packs you can fill, and 2 is what is still on the desk. The rows of dots are only text inside the quotes; they line the numbers up so the report is easy to read.
The compiler checks sums too
Arithmetic only makes sense on numbers, and the compiler knows which of your values are numbers. Put quotes around one and it becomes text, and the division is refused before the program runs:
const eggs = "45";
console.log(eggs / 6);main.ts(3,13): error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type.In that message, the left-hand side means whatever sits to the left of the /, which is eggs here. Of the types
the message lists, number is the only one this path uses. Types get an exercise of their own next.
Your turn
Nine hens each laid five eggs, and a box holds six. The program already multiplies the hens by the eggs each to get the total, and it already prints the report. Two lines are unfinished:
const fullBoxes = 0; // whole boxes - change this
const leftOver = 0; // eggs past those - and thisReplace each 0 with a sum built from eggs and perBox: the number of full boxes for fullBoxes, and the
eggs that do not make up a box for leftOver. Press Run and read the report back. Seven full boxes and three
eggs spare means it worked.
If something goes wrong
A decimal in the box count, as in Full boxes ....... 7.5, means the division is right but Math.floor is not
wrapped around it yet.
A box count of 45 with no message at all means two slashes where one was meant. Two slashes begin a comment,
so everything after them on that line is ignored, and fullBoxes quietly takes the whole egg count.
If nothing prints and the compiler reports Cannot find name 'math'. Did you mean 'Math'?, the capital M went
missing.
Nothing here can break. Change the number of hens and run it again: the whole report works itself out afresh. The check is looking for nine hens, so put 9 back to see it pass again.
Write your code
Runs in your browser. Press Run (or Ctrl/Cmd+Enter) and the output is checked for you.
Press Esc then Tab to move keyboard focus out of the code editor.
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 report then claims 7.5 full boxes. That is the right answer to a division and the wrong answer to a packing question, because half a box is not a full one. Division here never rounds down to a whole number for you.
- Using two slashes for whole-number division
- Some languages divide into whole numbers that way. In TypeScript two slashes start a comment, so the rest of the line is ignored and fullBoxes is left holding all 45 eggs. The compiler has no complaint, because a line that ends in a comment is ordinary.
- Writing math.floor with a small m
- Math with a capital M is the only spelling that exists, so the compiler stops before anything prints with Cannot find name 'math'. Did you mean 'Math'?
- Putting the two sums the wrong way round
- Nothing complains and the report prints neatly, but it says 3 full boxes and 7 eggs left over. Seven loose eggs would fill another box of six, which is the clue that the two operators have swapped places.
Want a blank editor instead? Open the TypeScript playground.