freeCodeCamp Challenge Guide: Sum All Numbers in a Range

Intermediate Solution states:

But is this true? Calling arr.sort() actually sorts the array in place, does it not? The variable sortedArr is initialized but never used–the original arr is used instead.

What I’m driving at is the sortedArr variable is extraneous and doesn’t need to be declared.

3 Likes

Can someone tell me how ineffective / effective my recursive solution is:

function sumAll(arr) {
  var min = Math.min(arr[0], arr[1]),
      max = Math.max(arr[0], arr[1]);
  
  if (min === max)
    return (min);
  return max + sumAll([min, max - 1]);
}

sumAll([1, 4]);

And generally, how can one measure code efficiency in js ?

Cheers!

8 Likes

en… why is no one using Math.abs()?

function sumAll(arr) {
  return (arr[0]+arr[1])*(Math.abs(arr[0]-arr[1])+1)/2;
}
9 Likes

My solution is similar, except instead of using the newer spread syntax, I opted for the classical apply function.

function sumAll(arr) {
  var n1 = Math.min.apply(null, arr);
  var n2 = Math.max.apply(null, arr);
  return ((n2 + n1) * (n2 - n1 + 1)) / 2;
}
3 Likes

I also thought the reduce method should be used. I agree the code examples here without it are simpler hence a good practice. But it was fun learning more about the reduce method:

function sumAll(arr) {
var newArr = [];
newArr[0] = Math.min(…arr);
var i = Math.min(…arr);
var j=0;

while (i<Math.max(arr[0],arr[1])){
newArr[++j] = ++i;
}
return newArr.reduce(function(acc, val){
return acc + val;
},0);
}

This is my solution :

function sumAll(arr) {
var max = Math.max(arr[0], arr[1]);
var min = Math.min(arr[0], arr[1]);
var newArr = [];

for (i=min; i<=max; i++) {
newArr.push(i);
}
return newArr.reduce(function(a, b) {
return a + b;
});
}

sumAll([5, 10]);

This is my code for this freecodecamp exercise:

I used reduce and it helped a lot.

function sumAll(arr) {

var max = arr.reduce(function(a, b) {
  return Math.max(a, b);
});
var min = arr.reduce(function(a, b) {
  return Math.min(a, b);
});
var temp = 0;

for(var i = min; i < max + 1; i++) {
  temp += i;
  console.log(temp);
}
return temp;

}

sumAll([4, 1]);

You are not alone :smiley: Look at mine :smiley:

1 Like

My solution:

function sumAll(arr) {
  //sort array so lowest comes first
  arr.sort(function(a,b){
    return b < a;
  });
  
  //create sum variable and assyign total sum of arr to it
  var sum = arr.reduce(function(acc, num){
    return acc + num;
  });
  //check if we need to add the sum of number between first and last element of an array
  if(arr[0] !== arr[1]){
    //create second array to store numbers between first and last element of an array
    var arr2 = [];
    var index = arr[0]+1;//set the index
    
    while(index < arr[1]){
      arr2.push(index);//add between numbers to arr2
      index++;
    }
    //assyign sum of arr to sum of arr2
    sum += arr2.reduce(function(acc, num){
      return acc + num;
    });
  }
  
  return sum;
}
//test
sumAll([1, 4]);

Here I am my code using the .sort method and a for loop.

function sumAll(arr) {
var sumArr = 0, sortArr = arr.sort(function(a,b){
return a - b;
});
for(var i = sortArr[0]; i < sortArr[1]; i += 2){
sumArr += i + (i + 1);
}
return sumArr;
}

sumAll([10, 5]);

But sincerely , if the suggestion to using reduce() is for learning then solution should include it. Remember its a leanring environment . As one who teaches at a university I cannot give my students a hint then not use it. It causes confusion and frustrates learners.

I went for

function sumAll(arr) {
  var max = arr.reduce(function(a, b) {
  return Math.max(a, b);
  });
  var min = arr.reduce(function(a, b) {
  return Math.min(a, b);
  });
  var result = 0;
  for (i = min; i <= max; i++){
     result += i;
  }
  return result;
}
2 Likes

A solution without using Math.max() and Math.min()

    function sumAll(arr) {
      var max;
      var min;
      var result = 0;
      if (arr[0] > arr[1]) {
        max = arr[0];
        min = arr[1];
      } else {
        max = arr[1];
        min = arr[0];
      }
      while (min <= max) {
        result = result + min;
        min++;
      }
      return result;
    }
2 Likes

Hehe, same code here. Congrats. btw. i < max + 1 is the same as i <= max; :wink:

My basic solution:

function sumAll(arr) {

arr.sort(function(a, b) {
return a - b;
});

for(var i = arr[0]+1; i < arr[1]; i++) {
arr.push(i);
}

return arr.reduce(function(val1, val2) {
return val1 + val2;
}, 0);
}

sumAll([5, 2]);

I “.sort()” the array. Loop and push new value in array. And, finally, I reduce.

2 Likes

You are using ES6 which does not work in the FC’s editor.

oh i see what you did there

Why not use some good old mathematics?

function sumAll(arr) {
  max=Math.max(arr[0],arr[1]);
  min=Math.min(arr[0],arr[1]);
  return (max+min)*(max-min+1)/2;
}
4 Likes

function sumAll(arr) {
return arr.sort(function(a,b){return a-b;}).reduce(function(a,b){
return ((b*(b+1))/2)-((a*(a-1)/2));

});
}

Is this code okay ?? It worked fine … But how efficient is this ??

1 Like

More or less came up with verbatim the advanced code solution

function sumAll(arr) {
  var result = 0;
  for (let i=Math.min(...arr); i<=Math.max(...arr); i++){
    result+=i;
  }
  return result;
}

sumAll([1, 4]);
2 Likes