given a non-negative integer, num, repeatedly adds the numbers in each bit until the result is a single digit.
For the sum problem of
leetcode, my idea is to divide the numbers and add them, recursively, and return the numbers less than 10
.
related codes
var addDigits = function(num) {
let str = num+=""
let len = str.split("").length;
let numTemp = 0
if(num < 10) {
console.log(num)//2
return num
} else {
for (let i =0; i<len;iPP) {
numTemp += parseInt(str.split("")[i], 10)
}
addDigits(numTemp)
}
};
console.log(addDigits(38)//undefined
what result do you expect? What is the error message actually seen?
2undefined,2