Sum All Numbers in a Range.Help

Sum All Numbers in a Range.Help
0

#1

Tell us what’s happening:

Where is my wrong?I need help to solve this problem.

Your code so far


function sumAll(arr) {
  
  var sum = 0;
  
  for(var i=0; i<arr.length; i++){
    
   sum += Math.max(arr[i]) + Math.min(arr[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/68.0.3440.106 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


#2

It solves some cases, but the rest is not solved, you can compare the elements and sort it out,

You are just adding the two elements of the var arr

So lets say 1,4 we can loop from 1 to 4 and add the elements, What you have done is finding the max and min value of a single element, in this line and adding through the loop,

which gives a correct answer for items like 1,4 because it flows in an order but what about 4,1, it is descending, therefore you cannot increment the loop, so the test fails for that case

Initially we don’t know which is the max or min element so
Hint: Compare the two elements of the arr and assign the max value to a var and similarly do this for min value and then run the loop from min to max and add those


#3

First, you need to determine what is the min value and what is the max value.

Be careful with Math.min and Math.max. It takes a sequence of numbers and not an array.

You can use the notation Math.min(…arr) to transform an array into a sequence. This is ES6 syntax.

But if you don’t understand what I mean, I suggest you create your own min and max function.

Then, make a loop between this min and this max and sum it all.


#4

use console.log(i)
Math.max(arr[i]) + Math.min(arr[i]) this the same as i+i, because arr[i] =1
because in first is 1+1 and second loop 4+4 =2+8=10