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

How to extract items from a subarray and push them to the main array

Detox gets stuck on JS Timer/Main Run Loop when registering a lot of screens with React Native Navigation Detox gets stuck on JS Timer/Main Run Loop when registering a lot of screens with React Native Navigation

\\u0000 cannot be converted to text error \\u0000 cannot be converted to text error