Code Working on JSFiddle but FCC editor rejecting

Code Working on JSFiddle but FCC editor rejecting
0

#1

Hi,

Kindly please suggest what to do for my task - https://www.freecodecamp.org/challenges/sum-all-numbers-in-a-range

My JSFIDDLE link - https://jsfiddle.net/jinisner/zou3m85q/

Working fine but same code on FCC Editor -

///=============================

function sumAll(arr) {
 var x = Math.min.apply(Math, arr);
  var y = Math.max.apply(Math, arr);
 for(var i = x; i<=y; i++){
   test.push(i);
 }
  
  function add(add1,value1){
    return add1 + value1;
  }
  
  return test.reduce(add,0);
  
}

sumAll([1, 4]);
//=====================================

Satisfies only one condition of returning a number , please help out here …

Regards


#2

You need to define test before pushing to it.


#3

Add var test = []; before your for loop.

Additionally, you don;t need to do any of pushing the numbers to the array and then reducing the array. You can use the same for loop to add numbers in a temp variable. Like

function sumAll(arr) {
  var x = Math.min.apply(Math, arr);
  var y = Math.max.apply(Math, arr);
  var sum = 0;
  for(var i = x; i<=y; i++){
    sum += i;
  }

  return sum;
}

#4

@TheGallery @adityaparab - thanks so much it solved the problem, just silly mistakes on my part …

Regards