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
- Declare
lastTwoDigitsasn % 100. Everything above the tens place disappears, so 2379 becomes 79. The result is always between 0 and 99. - Declare
lastDigitaslastTwoDigits % 10. This keeps only the ones place: 79 becomes 9. - Declare
secondLastDigitwithMath.floor(lastTwoDigits / 10). In TypeScript,/keeps the fraction, so79 / 10is 7.9.Math.floorrounds down to 7, which is the tens digit. - 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 / 10is 7.9 and7 / 2is 3.5. To get a whole-number quotient you round it yourself.Math.floorandMath.truncagree for non-negative values like these. - The result of
%has the sign of the left operand.-2379 % 100is-79, not 21. That is why the lesson restrictsnto non-negative integers, and whyMath.floorwould also behave differently for negatives. constfor values that don't change. Each variable is assigned once, soconstsays so and the compiler stops you from reassigning it.- Annotations are optional here.
: numberrepeats 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 % 100is 5. The tens digit isMath.floor(5 / 10), which is 0, so the sum is 5. The same formulas handle it without special cases. - Numbers ending in 00.
100gives0 + 0 = 0. - Exactly two digits.
45gives5 + 4 = 9, because45 % 100is 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
% 10twice.n % 10gives 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
lastTwoDigitsitself gives 79, not 16. The sum is of the separate digits.
Testing
n = 2379gives16.n = 45gives9.n = 100gives0.n = 2305gives5.