Javascript return undefined for an internal function Javascript return undefined for an internal function
I am doing an easy leetcode questions where we add one to a number whose digits are stored in an array. My logic works well but the returned value is undefined. Console gives the correct answer. Why? And how do I fix it?
var plusOne = function(digits) {
addNumber(digits.length - 1, 1, digits)
function addNumber(index, value, digitsArr) {
let sum = digitsArr[index] + value
if(sum > 9) {
let onesDigit = parseInt(sum.toString().charAt(1))
digitsArr[index] = onesDigit
let twosDigit = parseInt(sum.toString().charAt(0))
addNumber(index-1, twosDigit, digitsArr)
} else {
digitsArr[index] = sum
console.log(digitsArr)
return digitsArr
}
}
};
console.log(plusOne([1,2,9]))
from Stackoverflow
Comments
Post a Comment