Intermediate Algorithm Scripting - Sum All Numbers in a Range

Tell us what’s happening:
Can anyone shed some light on why below code does not pass?

Your code so far
function sumAll(arr){
let result=;
if(arr[0]<arr[1]){
for(let i=arr[0];i<=arr[1];i++){
result.push(i);
}
} else {
for(let j=arr[1];j>=arr[0];j–){
result.push(j);
}
} return result.reduce((a,b)=>a+b);
}

function sumAll(arr){
    let result=[];
    if(arr[0]<arr[1]){
        for(let i=arr[0];i<=arr[1];i++){
          result.push(i);
        } 
    } else {
        for(let j=arr[1];j>=arr[0];j--){
          result.push(j);
        } 
    } return result.reduce((a,b)=>a+b);
}

sumAll([1, 4]);

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36

Challenge: Intermediate Algorithm Scripting - Sum All Numbers in a Range

Link to the challenge:

I don’t understand why you are making an array?

this loop is your problem. But like I said above, I wouldn’t make an array at all.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.