Code examples

Pack lunch bags

Integer division counts full groups. The remainder tells you what is left.

Simple example

Start7 ÷ 3
Result2 1
lesson.ts
console.log(Math.floor(7 / 3));
console.log((7 % 3));
Output
2
1
Advertisements

Everyday examples

You have 17 apples. Each lunch bag holds 5. Show the number of full bags, then the apples left over.

Start17 ÷ 5
Result3 2
lesson.ts
const apples: number = 17;
const per_bag: number = 5;
console.log(Math.floor(apples / per_bag));
console.log((apples % per_bag));
Output
3
2
Run the example
Terminal
tsc --target ES2020 lesson.ts
node lesson.js
Official documentation