1

Following is my code in which is working fine for smaller array number, but if I am using large size number of array in the method it is giving weird answers -

Code -

function upArray(arr){
  var fail = false,
      count = '';
  arr.map(function(x){
    if(x>10 || x<0 || x== null || x== undefined)
      fail = true;
    else {
      count += x.toString();
      fail = false;
    }
  })
  if(arr.length < 1)
    fail = true;
  return fail ? null : (parseInt(count)+1).toString().split("").map(function(x){return parseInt(x);});
}

Correct result -

console.log(upArray([2,4,5])); //[2,4,6]
console.log(upArray([2,5,1])); //[2,5,2]
console.log(upArray([2,8,9])); //[2,9,0]

BUT - If I am giving a large array of numbers, its giving weird results -

like -

console.log(upArray([9,2,2,3,3,7,2,0,3,6,8,5,4,7,7,5,8,0,8]));

giving - [9, 2, 2, 3, 3, 7, 2, 3, 6, 8, 5, 4, 7, 7, 5, 8, 0]

Let me know what I am doing wrong over here and what is the fix ?

Tech Solvr
  • 505
  • 1
  • 7
  • 25

5 Answers5

1

Your parseInt(count)+1 is exceeding the maximum safe integer in JavaScript, Number.MAX_SAFE_INTEGER, which gives the unexpected results.

A possible working implementation could be:

function upArray(arr){
  var result = [],
      remaining = 1;
  while (arr.length) {
    var x = arr.pop();
    if (x > 10 || x < 0 || x == null) { // x == null works for undefined as well
      return null;
    }
    if (remaining) {
      var _x = x + remaining;
      x = _x % 10;
      remaining = Math.floor(_x / 10);
    }
    result.unshift(x);
  }
  if (remaining) {
    result.unshift(remaining);
  }
  return result;
}

This works because it increments the integers one by one starting from the right untill there is no overflow (overflow is from 9 to 0).

CrocoDillon
  • 126
  • 5
0

The input you are giving it is larger than the largest integer JavaScript supports, which is +/- 9007199254740991. It only supports 53-bit integers. See What is JavaScript's highest integer value that a Number can go to without losing precision?.

Community
  • 1
  • 1
danmcardle
  • 2,079
  • 2
  • 17
  • 25
0

Like danmcardle noted your integer is too big. Have a look at this SO question: What is JavaScript's highest integer value that a Number can go to without losing precision?

Furthermore your code could be optimized a lot I think.

function upArray(nr){
  nr++;
  var nrString = nr.toString();
  return nrString.split('');
}
Community
  • 1
  • 1
timr
  • 6,668
  • 7
  • 47
  • 79
0

What you're doing is asking for trouble.

You are:

  1. Mapping an array of integers into single-char strings and concatenating
  2. Parsing the result into an integer
  3. Adding 1 to the integer
  4. Converting the result into a string and splitting back into a single-char array
  5. Parsing each character back to an integer.

Your problem occurs when you attempt to do this with an array of integers that would concatenate into the integer 9223372036854775808. This is larger than Number.MAX_SAFE_INTEGER, so your call to parseInt(count) will not return what you expect it to. (9223372036854776000 in this case).

Also, accepted best practice is to avoid == and != completely. Always use the strict quality operators === and !==.

Alex McMillan
  • 17,096
  • 12
  • 55
  • 88
-1

Two problems:

  1. Your last value is larger than the maximum integer value supported by JavaScript
  2. Even on smaller numbers, you'll throw away 0s within the array, since 0 == null and 0 == undefined. You need to use === to check for value and type equality.
if (x > 10 || x < 0 || x === null || x === undefined)
Community
  • 1
  • 1
Paul Roub
  • 36,322
  • 27
  • 84
  • 93