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

Popular posts from this blog

With jQuery, how to automatically select the value of an input text that has been modified with the arrows? With jQuery, how to automatically select the value of an input text that has been modified with the arrows?

Cannot create an instance of a defined class in Javascript Cannot create an instance of a defined class in Javascript

Using Ts-Standard linting, JSDoc comment at beginning of file triggers "This rule requires the `strictNullChecks` compiler option" error Using Ts-Standard linting, JSDoc comment at beginning of file triggers "This rule requires the `strictNullChecks` compiler option" error