Start learning free

LESSON 57 OF 348 · CALCULATIONS - CHALLENGES

Last Two Digit Sum in TypeScript: Modulo and Math.floor

Practice Read the full explanation

Given a non-negative integer n with at least two digits, compute and return the sum of the last two digits

Example:

Input: n = 2379

Output: 16

Explanation:
Last two digits of n are 7 and 9, so our answer is 7 + 9 = 16
Hints:

You can make use of modulo operator

You can get the last digit of a number using modulo 10:
2379 % 10 = 9

You can get a number consisting of last 2 digits using modulo 100:
2379 % 100 = 79

If you have a 2 digit number, you can get the first digit by using integral division

Divide a 2 digit number to 10 to get the first digit:
79 / 10 = 7
STEP 1 OF 4
    Ctrl + Enter to check

    TYPESCRIPT · CALCULATIONS - CHALLENGES

    Last Two Digit Sum in TypeScript, explained

    Problem overview

    The method lastTwoDigitSum receives a non-negative integer n with at least two digits and returns the sum of its last two digits. For n = 2379 the last two digits are 7 and 9, so the answer is 16.

    Approach: peel digits off with remainders

    A number does not store its digits separately, so you get them with arithmetic. The remainder operator % returns what is left after a division. Dividing by 10 leaves the last digit, and dividing by 100 leaves the last two digits together:

    2379 % 100 = 79
    79 % 10 = 9
    

    That gives the last digit, 9. The other digit, 7, is the tens digit of 79. It is what you get by dividing 79 by 10 and throwing away the fraction.

    The plan has three parts: isolate the last two digits with % 100, take the last digit of that with % 10, and take the first digit with a division that drops the fraction. Then add the two digits.

    Step by step

    1. Declare lastTwoDigits as n % 100. Everything above the tens place disappears, so 2379 becomes 79. The result is always between 0 and 99.
    2. Declare lastDigit as lastTwoDigits % 10. This keeps only the ones place: 79 becomes 9.
    3. Declare secondLastDigit with Math.floor(lastTwoDigits / 10). In TypeScript, / keeps the fraction, so 79 / 10 is 7.9. Math.floor rounds down to 7, which is the tens digit.
    4. Return lastDigit + secondLastDigit. Both are numbers, so + adds them.

    The solution in TypeScript

    class Solution {
        lastTwoDigitSum(n: number): number {
            const lastTwoDigits: number = n % 100;
            const lastDigit: number = lastTwoDigits % 10;
            const secondLastDigit: number = Math.floor(lastTwoDigits / 10);
            return lastDigit + secondLastDigit;
        }
    }
    

    Some details worth knowing:

    • There is no integer division operator. All numbers are doubles, so / always gives the exact quotient: 79 / 10 is 7.9 and 7 / 2 is 3.5. To get a whole-number quotient you round it yourself. Math.floor and Math.trunc agree for non-negative values like these.
    • The result of % has the sign of the left operand. -2379 % 100 is -79, not 21. That is why the lesson restricts n to non-negative integers, and why Math.floor would also behave differently for negatives.
    • const for values that don't change. Each variable is assigned once, so const says so and the compiler stops you from reassigning it.
    • Annotations are optional here. : number repeats what the compiler would infer. It is checked when compiling and erased from the JavaScript that runs, so it can't convert or validate a value at run time.

    Seeing the pieces

    You can print each intermediate value for n = 2379 to watch the digits separate:

    const n: number = 2379;
    console.log(n % 100);
    console.log(79 / 10);
    console.log(Math.floor(79 / 10));
    

    This prints:

    79
    7.9
    7
    

    The first line is the two-digit number, the second shows why the fraction has to be removed, and the third is the tens digit that remains after rounding down. Adding the ones digit 9 to it gives the answer, 16.

    The method works on a plain number, so it never converts n to a string. A string-based approach, which splits the text of n into characters, also works, but it needs conversions back to numbers and gains nothing here.

    Edge cases and common mistakes

    • Zero in the tens place. For 2305, 2305 % 100 is 5. The tens digit is Math.floor(5 / 10), which is 0, so the sum is 5. The same formulas handle it without special cases.
    • Numbers ending in 00. 100 gives 0 + 0 = 0.
    • Exactly two digits. 45 gives 5 + 4 = 9, because 45 % 100 is still 45.
    • Forgetting the rounding. Without Math.floor, the second digit comes out as 7.9, and the sum is 16.9 instead of 16.
    • Using % 10 twice. n % 10 gives the last digit only. To reach the second-to-last digit you must first drop the last one, which is what dividing by 10 does.
    • Adding the wrong values. Adding lastTwoDigits itself gives 79, not 16. The sum is of the separate digits.

    Testing

    • n = 2379 gives 16.
    • n = 45 gives 9.
    • n = 100 gives 0.
    • n = 2305 gives 5.
    Keep going with TypeScriptAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free