Sum all numbers in a range - numbers concatenating instead of adding up

Sum all numbers in a range - numbers concatenating instead of adding up
0

#1

why are my numbers concatenation and not adding up?

function sumAll(arr) {
  var ans = arr.reduce(function(a, b){
    var min = Math.min(a, b);
    var max = Math.max(a, b);
    var result = "";
    for(  min = min;min <= max; min++){
      result += min;
    }
    return result;
  });
  return ans;
}

sumAll([1, 4]);

#2

I’ve edited your post for readability. When you enter a code block into the forum, remember to precede it with a line of three backticks and follow it with a 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.

I changed the title of your topic to reflect the correct challenge name and the problem you are having.

To answer you question, since you initialize result as an empty string, when you have the line:

result += min;

the min value gets treated as if it were a string, because result is a string. Why don’t you initialize result as 0 (zero)?


#3

Wow Thank You, that was easy. it was my first time building up the courage to post, I really appreciated it.