Home > Software engineering >  Javascript: Plus one challenge
Javascript: Plus one challenge

Time:10-27

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]

CodePudding user response:

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);

CodePudding user response:

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();
};

CodePudding user response:

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.

CodePudding user response:

If you put a console.log(num) after the let num = line you will see it. The NaN matches up with a decimal point and an e .

Since you're already getting an array of numbers, you could just manually add 1 to the last index like this:

    
    var plusOne = function(digits) {
        const lastIndex = digits.length - 1
        digits[lastIndex] = digits[lastIndex]   1
        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]));

  • Related