I’m working on a series of tasks in which I need to implement a function that takes 2 integers and returns the sum of every number between(and including) them. I wanted to use a spread operator to initially convert the function parameters into array and then use a loop to sum all the numbers between the min and max value. For some reason, it’s not working and it’s returning only the max value. Please help me figure out where’s the issue?
function sumAll(...numbers) {
let max = Math.max(...numbers);
let min = Math.min(...numbers);
let sum=0;
for (let i= min; i<=max; i++) {
sum =+ i;
}
return sum
}
sumAll (1, 4)
Right. But sum =+ i is taken by the js interpreter as “take this i thing, whatever it is, and coerce it into an integer (that’s what the + operator is doing there), then assign that value to sum.” Not adding, but overwriting.