# Not sure how to proceed

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`.

Challenge: Sum All Numbers in a Range

1. How would you do this by hand? What would you literally do, with a pencil and paper?

2. How can you represent that exact same processes with loops?

3. After you have this working with a raw `for` loop, how would you refactor that `for` loop to use a high order method?

Step 2 should come before step 3.

1 Like

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

This is what you would literally do with pencil and paper?

Please. Forget about the high order methods. You need correct code before you can refactor to elegant code.

1 Like

Ok I’d count from one number to the other and add each to the last. So 5+6+7+8+9+10 = 45

That’s exactly what I’d do too. So how can you write a `for` loop that does exactly that?

Hi @am93 !

Why use reduce for math.max?

I know you are not a fan of MDN docs but I would highly suggest looking at their example of how to get a max number from an array.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/max

1 Like

`for (let i = min; i <= max; i++)`

That’s exactly the loop head I’d use. So what loop body would do the adding?

Maybe the following

``````let sumOfNums = 0;
for (let i = min; i <= max; i++) {
return i + sumOfNums;
}
``````

I actually did use that to get what I included unfortunately. Their example was

``````var arr = [1,2,3];
var max = arr.reduce(function(a, b) {
return Math.max(a, b);
});
``````

This `return` is inside of the loop body again. Do you remember what happens the first time your function encounters a `return` statement?

When I look at it I see this

``````const array1 = [1, 3, 2];

console.log(Math.max(...array1));
// expected output: 3

``````

I also see the example with reduce but then it talks about the use of the spread operator.

1 Like
``````let sumOfNums = 0;
for (let i = min; i <= max; i++) {
sumOfNums += i;
}
return sumOfNums;
}
``````

Ah I see, sorry I’m not surw what happened on my end then

Ok, that’s getting closer. Now, this won’t run as is because you haven’t defined `max` and `min` yet. How can you get those?

1 Like

Also, not to further complicate your life but you could also just sort the original arr and know the min and max numbers from there.

Just an alternative to Math.Max and min

1 Like

Got it. Thank you guys! Amazing touters

1 Like

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.

1 Like