Hi I just started intermediate algorithms and I’m not able to get even the first part of my code working. Please guide me to how to do this, I wrote my thought process as comments. As of right now Math.max is returning 5… Thanks
**Your code so far**
function sumAll(arr) {
let maxNum = arr.reduce((arr) => {
return Math.max(arr);
});
console.log(maxNum);
}
console.log(sumAll([5, 10]));
// find min
// find max
// loop from min to max
// return sum of all numbers between min and max including min and max
**Your browser information:**
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36.
I’d count between min (Math.min) and max (Math.max) and see what numbers come in between (loop), then I’d add them all up (store it all as a array, then use reduce to add them up) which is what I commented. Does that sound right?
If it sounds right then I can’t even get Math.max right and I don’t know why
Yep, with some destructuring to pull out the two values.
I really wish JavaScript had range iterators. With a range iterator, we could do a nice, clean high order function solution. Without it, there isn’t a good, memory efficient approach that comes to mind.