Code works but i won't pass the test

Can someone please explain to me why i can’t get this test to pass. The numbers i get as result are the same numbers they ask for the solution of the test.So my code works. I just don’t get it. Is it because i use “.push” which mutates the original array? Thanks for helping.

Your code so far


let newArr = [];
function sumAll(arr) {

if(arr[0] > arr[1]){
  newArr.push(arr[0]);
  
  arr[0]--;
  return sumAll(arr);
}
else if(arr[0] < arr[1]){
  newArr.push(arr[0]);
  arr[0]++;
  return sumAll(arr);
}
else{
  newArr.push(arr[0]);
  
}
return newArr.reduce((first, second)=> {
  return first + second;
});
}

console.log(sumAll([10, 5]));

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36.

Challenge: Sum All Numbers in a Range

Link to the challenge:

Your code contains global variables that are changed each time the function is run. This means that after each test completes, subsequent tests start with the previous value. To fix this, make sure your function doesn’t change any global variables, and declare/assign variables within the function if they need to be changed.

Example:

var myGlobal = [1];
function returnGlobal(arg) {
  myGlobal.push(arg);
  return myGlobal;
} // unreliable - array gets longer each time the function is run

function returnLocal(arg) {
  var myLocal = [1];
  myLocal.push(arg);
  return myLocal;
} // reliable - always returns an array of length 2

Okay, that makes sense of course. I did not test it with more than 1 function call. :slight_smile: Thanks!