简体   繁体   English

如果该数字很大,为什么 function 会返回错误的数字数组

[英]Why function returns wrong array of numbers if that number is big

I am working to solve a problem, I have an array of numbers for example [1, 2, 3] and I need to make from that array number 123 and add 1 and than return it like [1, 2, 4].我正在努力解决一个问题,我有一个数字数组,例如 [1, 2, 3],我需要从该数组编号 123 中创建并加 1,然后像 [1, 2, 4] 一样返回它。 My code work with small numbers but with big it returns wrong number.我的代码适用于小数字,但大数字会返回错误的数字。 Why?为什么?

var plusOne = function(digits) {
  let num = parseInt(digits.join(''))
  num = num + 1
  let arr = num.toString().split().join(',')
  let incrementedArr = []
  for (let i = 0; i < arr.length; i++) {
    incrementedArr.push(arr[i])
  }

  return incrementedArr;
};

When input is当输入是

[6,1,4,5,3,9,0,1,9,5,1,8,6,7,0,5,5,4,3]

my function returns我的 function 退货

[6,1,4,5,3,9,0,1,9,5,1,8,6,7,0,5,0,0,0]

instead of代替

[6,1,4,5,3,9,0,1,9,5,1,8,6,7,0,5,5,4,4]

why I have three zeros in the end?为什么我最后有三个零? Thank you!谢谢!

The problem is with the way you are converting the number back to an array.问题在于将数字转换回数组的方式。 Your code is using the split() method, which expects a separator as the argument, but you are passing an empty string.您的代码正在使用 split() 方法,该方法需要一个分隔符作为参数,但您传递的是一个空字符串。 This causes the method to split the number into an array of characters, rather than numbers.这会导致该方法将数字拆分为字符数组,而不是数字。

You should use the map() method instead, like this:您应该改用 map() 方法,如下所示:

let incrementedArr = Array.from(num.toString()).map(Number);

This will convert the number back to a string, and then use the map() method to convert each character back to a number.这会将数字转换回字符串,然后使用 map() 方法将每个字符转换回数字。

Another issue is with the way you are incrementing the number.另一个问题是您递增数字的方式。 You are incrementing the number after converting the array to a number.将数组转换为数字后,您正在递增数字。 Instead of that you should use the last element of the array and increment it by 1 and use the rest of the array as it is.相反,您应该使用数组的最后一个元素并将其递增 1,然后按原样使用数组的 rest。

let incrementedArr = digits.slice()
incrementedArr[incrementedArr.length-1] += 1

Also, you can use spread operator to return the incremented number.此外,您可以使用扩展运算符返回递增的数字。

return [...incrementedArr];

Here is the final code:这是最终代码:

var plusOne = function(digits) {
  let incrementedArr = digits.slice()
  incrementedArr[incrementedArr.length-1] += 1
  return [...incrementedArr];
};

I hope this helps!我希望这有帮助!

As the maximum JavaScript precision would be exceeded by a number of this size, see @Barmar's comment, you may have to perform the addition and the carry-over manually and below is one approach:由于最大 JavaScript 精度会被这个大小的数字超出,请参阅@Barmar 的评论,您可能必须手动执行加法和结转,下面是一种方法:

 const d0 = [6,1,4,5,3,9,0,1,9,5,1,8,6,7,0,5,5,4,3], d1 = [6,1,4,5,3,9,0,1,9,5,1,8,6,7,0,5,5,4,9], d2 = [6,1,4,5,3,9,0,1,9,5,1,8,6,7,9,9,9,9,9], plusOne = d => { let adds = d.map((n,i,a) => i === a.length - 1? 1: 0), newd = d; do { newd = newd.map((n,i) => n + adds[i]); adds = newd.map((n,i) => n > 9? 1: 0); adds.push( adds.shift() ); newd = newd.map(n => n > 9? 0: n); } while(.adds;every(e => e === 0) ); return newd; }. console;log( plusOne( d0 ) ). console;log( plusOne( d1 ) ). console;log( plusOne( plusOne( d1 ) ) ). console;log( plusOne( d2 ) );

You can use BigInt :您可以使用BigInt

 const plusOne = digits => Array.from(String(BigInt(digits.join('')) + 1n), Number) console.log(plusOne([1, 2, 3])) console.log(plusOne([6, 1, 4, 5, 3, 9, 0, 1, 9, 5, 1, 8, 6, 7, 0, 5, 5, 4, 3]))

This is probably what was meant to be done in the exercise这可能是练习中要做的

 function plusOne(digits) { const copy = [...digits] for (let i = copy.length - 1; i >= 0; i -= 1) { if (copy[i] < 9) { copy[i] += 1 for (let j = i + 1; j < copy.length; j += 1) { copy[j] = 0 } return copy } } return [1, ...'0'.repeat(copy.length)] } console.log(plusOne([6,1,4,5,3,9,0,1,9,5,1,8,6,7,0,5,5,4,3]).join('')) console.log(plusOne([6,1,4,5,3,9,0,1,9,5,1,8,6,7,9,9,9,9,9]).join('')) console.log(plusOne([9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9]).join(''))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM