Sum all numbers challenge not passing

Hi, can someone help me with this algorithm pls ? it logs out the correct answers for the conditions in the challenge but when i return the output it does not pass any conditions . what is wrong in my program?
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/sum-all-numbers-in-a-range

You will need to post your code as a reply.

To enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.

markdown_Forums

1 Like

sure thanks.

function sumAll(arr) {
  var lower = arr[0];
  var upper = arr[1];
  var sum = [];
  if (lower < upper){
  while (lower <= upper){
    sum.push(lower++);
  }
  console.log(sum);
  } else {
    while (lower >= upper){
      sum.push(lower--);
    }
    console.log(sum);
  }
  var add = function(x,y){
    return x+y;
  }
  console.log(sum.reduce(add)) ;
}

sumAll([1, 4]);

The instructions state “Return the sum of those two numbers plus the sum of all the numbers between them.” Your function displays the sum to the browser console but does not return the value to the calling function.

When I use return the challenge fails . I am still not able to figure out what is wrong with my code . even though it logs out the correct answer to the console , it fails to return the same.
could you pls help?

@guru15 Can you please post your latest code?

function sumAll(arr) {
  var lower = arr[0];
  var upper = arr[1];
  var sum = [];
  if (lower < upper){
  while (lower <= upper){
    sum.push(lower++);
  }
  return sum;
  } else {
    while (lower >= upper){
      sum.push(lower--);
    }
    return sum;
  }
  var add = function(x,y){
    return x+y;
  }
  return sum.reduce(add)) ;
}

sumAll([1, 4]);

Your original code was only missing a return statement at the end of the function. Your latest code adds two return statements. One is needed and the other one causes the solution to fail.

Thank you .it passed now.