can someone help me see where I’m going wrong here? I think my logic is correct I just think I may have a minor bug within my implementation.
Thank you!
Your code so far
function sumAll(arr) {
//set sum = 0
let sum = 0
// find min and max
sum += Math.max(arr);
sum += Math.min(arr)
console.log(sum)
//set var = math.min
let middle = (Math.min(arr) + 1)
// while var < math.max
// add var to sum
while (middle < Math.max(arr)) {
sum += middle
}
return sum
}
console.log(sumAll([1, 4]));
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36.
function sumAll(arr) {
//set sum = 0
let sum = 0
// find min and max
sum += Math.max(arr);
sum += Math.min(arr)
console.log(sum)
//set var = math.min
let middle = (Math.min(arr) + 1)
// while var < math.max
// add var to sum
while (middle < Math.max(arr)) {
sum += middle
middle++
}
return sum
}
I’m not quite sure how to use that tool. I have the function being logged but still not returning a number. can anyone help?
function sumAll(arr) {
//set sum = 0
let sum = 0
// find min and max
sum += Math.max(arr);
sum += Math.min(arr);
console.log(sum)
//set var = math.min
let middle = (Math.min(arr) + 1);
// while var < math.max
// add var to sum
while (middle < Math.max(arr)) {
sum += middle
middle++
}
return sum
}
console.log(sumAll([1, 4]));
The console is showing NaN. So the question is… why?
You have three lines with sum preceding this.
let sum = 0
This line looks benign.
sum += Math.max(arr);
sum += Math.min(arr);
So the issue must be here. But why?
Well, let’s look up Math.max() and Math.min().
Hmm…
The Math.max() function returns the largest of the zero or more numbers given as input parameters, or NaN if any parameter isn’t a number and can’t be converted into one.
NaN means that the input was not a number and can’t be converted to one…
Let’s look at the provided example in the documentation…
So you use use the spread syntax mainly when you have an array and you want to break it into a comma separated list of values, such as when passing them into a function or copying them into a new array.
It also comes in handy for destructuring in some cases.