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
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]);
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?
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]);
Thank you .it passed now.