Start learning free

LESSON 25 OF 348 · CALCULATIONS WITH NUMBERS

Free Coffee Cups in TypeScript: Counting Groups with Math.floor

Lesson Read the full explanation

For each of the 6 coffee cups I buy, I get a 7th cup free. In total, I get 7 cups.

Knowing that I paid for cups cups, compute and print the total number of cups I would get (number of cups I paid for + number of cups I got for free).


Example:

For example, if cups was 12, the answer would be 14.

Why? Because I paid for 12 cups and I got 2 for free, which is a total of 14 cups.

STEP 1 OF 1
    Ctrl + Enter to check

    TYPESCRIPT · CALCULATIONS WITH NUMBERS

    Free Coffee Cups in TypeScript, explained

    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

    1. Divide by 6: cups / 6 is 35.5.
    2. Round down with Math.floor, so that only complete groups are counted: Math.floor(cups / 6) is 35.
    3. Add the paid cups: 213 plus 35 free cups is 248.
    4. 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 to cups, 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.
    • const versus let. The starter code uses const because cups never changes. Your code reads it but does not assign to it, so const is fine.
    • Exact multiples. When cups is a multiple of 6, such as 12, the division is already a whole number, and Math.floor leaves 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 / 6 prints 248.5 for 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 = 0 gives 0.
    • cups = 5 gives 5, because there is no full group.
    • cups = 6 gives 7.
    • cups = 12 gives 14.
    • cups = 13 gives 15.
    • cups = 213 gives 248.

    Key takeaways

    • To count complete groups, divide and round down: Math.floor(total / size).
    • Choose the rounding function from the story: Math.floor for whole groups only, Math.ceil when a partial group still counts.
    • Test the boundaries, just below and exactly at a multiple of 6.
    Keep going with TypeScriptAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free