Problem overview
A coffee shop gives a free 7th cup for every 6 cups you buy. You know how many cups you paid for, stored in the constant cups, and the task is to print the total number of cups you receive: the paid ones plus the free ones.
const cups: number = 213;
In the task's example, 12 paid cups make two full groups of 6, so you get 2 free cups and 14 in total. Only complete groups count. With 5 paid cups you get no free cup, and with 13 you still get just 2.
Approach
The question is "how many full groups of 6 are in cups?". That is the quotient of cups divided by 6, the whole part of the division, and each group earns one free cup. The total is therefore:
total = cups + (number of full groups of 6)
Dividing in TypeScript keeps the decimals, so cups / 6 is 35.5 for 213 cups. The half group is not a group, and you must discard the fraction. Math.floor rounds down to the nearest whole number, which turns 35.5 into 35.
Step by step
- Divide by 6:
cups / 6is 35.5. - Round down with
Math.floor, so that only complete groups are counted:Math.floor(cups / 6)is 35. - Add the paid cups: 213 plus 35 free cups is 248.
- Print the total with
console.log.
const cups: number = 213;
console.log(cups + Math.floor(cups / 6));
It prints:
248
Details worth knowing
- The parentheses come from the function call. The division happens inside
Math.floor(...)before the result is added tocups, so no extra parentheses are needed. - There is no integer division operator. Some languages round the quotient of two integers automatically. In TypeScript,
/always gives a floating-point result, so the rounding is your job. constversuslet. The starter code usesconstbecausecupsnever changes. Your code reads it but does not assign to it, soconstis fine.- Exact multiples. When
cupsis a multiple of 6, such as 12, the division is already a whole number, andMath.floorleaves it unchanged.
There is also a way to get the same count with the remainder operator. Since cups % 6 is the number of cups left over after the full groups, subtracting it leaves a multiple of 6, and dividing that by 6 is exact: (cups - (cups % 6)) / 6 is also 35 for 213 cups. It does the same job with more steps, so Math.floor is the simpler choice.
Common mistakes
- Forgetting to round.
cups + cups / 6prints248.5for 213 cups, which is not a number of cups. - Using
Math.round. It rounds halves up, so 213 cups would give 249. It also gives wrong answers for 5 cups (6 instead of 5), because 5 / 6 rounds up to 1. A free cup is earned only by a complete group. - Using
Math.ceil. It rounds up, so 13 cups would give 16 instead of 15. - Dividing by 7. The group size for buying is 6, not 7: the 7th cup is the free one.
- Printing only the free cups. The task asks for the total, so the paid cups must be added in.
Testing
cups = 0gives0.cups = 5gives5, because there is no full group.cups = 6gives7.cups = 12gives14.cups = 13gives15.cups = 213gives248.
Key takeaways
- To count complete groups, divide and round down:
Math.floor(total / size). - Choose the rounding function from the story:
Math.floorfor whole groups only,Math.ceilwhen a partial group still counts. - Test the boundaries, just below and exactly at a multiple of 6.