(Help me for my mistake thankss)Sum All Numbers in a Range

Tell us what’s happening:
Why this method isnt working? Is there any logic mistake i make?

Your code so far

function sumAll(arr) {
  let sum = 0;

  if(arr[0] < arr[1]){
  for(let i = arr[0]; i<= arr[1];i++){
    sum += i;
  }
   } else{
    for(let i = arr[1]; i>= arr[0];i--){
    sum += i;
  }
}
 return sum;
}

sumAll([1, 4])



function sumAll(arr) {
  let sum = 0;

  if(arr[0] < arr[1]){
  for(let i = arr[0]; i<= arr[1];i++){
    sum += i;
  }
   } else{
    for(let i = arr[1]; i>= arr[0];i--){
    sum += i;
  }
}
 return sum;
}

sumAll([1, 4])

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/sum-all-numbers-in-a-range

I’ve edited your post for readability. When you 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 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

This is executed if arr[0] >= arr[1] right?
Read carefully the loop and think if it would ever work

Thankss, will keep this in mind next time

Yaa, still cant figure it out
Which mistake did i made?

Thankss guys I figured it out !

1 Like