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: