function sumAll(arr) {
var sum = 0;
var min = Math.min(arr);
var max = Math.max(arr);
for(var i = min; i <= max; i++){
sum += i;
}
return sum;
}
sumAll([1, 4]);

Math.min and Math.max accept arguments which need to be numbers. arr is an array, so since an array is not a number, each of the functions returns the value NaN. This causes the for loop to not execute the code inside, so sum remains 0.

I will give you two hints on how you can get the correct values of min and max.

One way would be to check if the first element of arr is greater than the second element of arr. If it is, assign max the value of the first element and min the value of the second element. Otherwise, the max should be assigned the value of the second element and min the value of the first element.

Another way is to look into how you could use the apply function with the Math.max and Math.mix functions to allow you to use arr’s elements as arguments/values for the functions.

function sumAll(arr) {
var sum = 0;
var min;
var max;
if(arr[0] < arr[1]){
min = arr[0];
max = arr[1];
}else{
min = arr[1];
max = arr[0];
}
for(var i = min; i <= max; i++){
sum += i;
}
return sum;
}
sumAll([1, 4]);

Another way to write the above code is to assume the first element is the min and the second element is the max and only switch them if the first element is greater than the second element. This eliminates the need for the else statement.

min = arr[0];
max = arr[1];
if(arr[0] > arr[1]){
min = arr[1];
max = arr[0];
}

you will need to tell also what is failing. What error do you get. What are the requirements?

In the future, please create your own topic when you have specific questions about your own challenge code. Only respond to another thread when you want to provide help to the original poster of the other thread or have follow up questions concerning other replies given to the original poster.

The easiest way to create a topic for help with your own solution is to click the Ask for Help button located on each challenge. This will automatically import your code in a readable format and pull in the challenge url while still allowing you to ask any question about the challenge or your code.