-1

You are given a large integer represented as an integer array digits, where each digits[i] is the ith digit of the integer. The digits are ordered from most significant to least significant in left-to-right order. The large integer does not contain any leading 0's.

Increment the large integer by one and return the resulting array of digits.

My solution:

/**
 * @param {number[]} digits
 * @return {number[]}
 */
var plusOne = function(digits) {
    let num = Number(digits.join('')) + 1
    const myFunc = x => Number(x);
    digits = Array.from(String(num), myFunc)
    return digits
};

console.log(plusOne([1,2,3,5,6,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7]));

Why does the above code not work given the following argument:

[1,2,3,5,6,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7]

my output:

[1,NaN,2,3,5,6,7,7,7,7,7,7,7,7,7,7,7,7,NaN,NaN,2,1]

expected output:

[1,2,3,5,6,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,8]
node_modules
  • 4,790
  • 6
  • 21
  • 37
  • 7
    [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) and [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173) – VLAZ Oct 26 '22 at 14:46
  • 4
    Have you logged the value of `num`? It might explain why your output contains 'NaN' – Reyno Oct 26 '22 at 14:46

3 Answers3

2

Since your number is larger then MAX_SAFE_INTEGER, I'd convert the array to a BigInt so you can add 1 to that (++), this way a 9 will be converted to the expected 10


After that, use the same technique to convert it back to an array:

var plusOne = function(digits) {
     let bigInt = BigInt(digits.join(''));
     bigInt++;
     return Array.from(bigInt.toString(), Number);
};

console.log(plusOne([1,2,3,5,6,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7]));
// [ 1, 2, 3, 5, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 8 ]

This can be re-written as a fancy one-liner like so:

const plusOne = (digits) => Array.from((BigInt(digits.join('')) + 1n).toString(), Number);
0stone0
  • 34,288
  • 4
  • 39
  • 64
0

Here is a implementation.

var plusOne = function (digits) {

    let carry = 0;
    let arr = [];

    for (let i = digits.length - 1; i >= 0; i--) {
        let n = Number(digits[i]);
        if (i === digits.length - 1) {
            n = n + 1;
        } else if (carry === 1) {
            n = n + 1;
        }
        if (n === 10) {
            carry = 1;
            n = 0;
        }

        arr.push(n)
    }
    return arr.reverse();
};
0

Looks like someone else already gave basically the same answer as I typed this, but here's my answer anyway.

It looks like you're getting this error, because the Number(digits.join('')) + 1 returns 1.2356777777777777e+21. Running Array.from(String(1.2356777777777777e+21)) by itself returns [1,.,2,3,5,6,7,7,7,7,7,7,7,7,7,7,7,7,e,+,2,1]. Notice you have 3 NaN values at positions 1, 18, and 19. In other words, you can't convert ".", "+", or "+" to numbers with Number(). It just returns NaN, as they are Not a Number...

Long story short—unless I misunderstand the point in the original question—you're going about it all wrong. Because your number is so big, you should use the BigInt() object. Note: It can only operate with other BigInts. Here's a working example:

const plusOne = digits => {
  const num = BigInt(digits.join('')) + BigInt(1)
  return num.toString().split('')
}

console.log(plusOne([1,2,3,5,6,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7]))

I hope that helps.

andrilla
  • 339
  • 1
  • 13