Add Two Digits
Description
You are given a two-digit integer n
. Return the sum of its digits.
Example
For n = 29
, the output should be
addTwoDigits(n) = 11
.
Input/Output
-
[execution time limit] 4 seconds (js)
-
[input] integer n
A positive two-digit integer.
Guaranteed constraints:
10 \leq n \leq 99
. -
[output] integer
- The sum of the first and second digits of the input number.
[JavaScript (ES6)] Syntax Tips
1
2
3
4
5
6
// Prints help message to the console
// Returns a string
function helloWorld(name) {
console.log("This prints to the console when you Run Tests");
return "Hello, " + name;
}
Solution
1
2
3
function addTwoDigits(n) {
return String(n).split('').map(Number).reduce((a, b) => a + b);
}