Sum All Numbers in a Range :

It seems when I pass an array into the Math.max(); or Math.min(); I receive a blank console. Is this wrong?

For example:

function sumAll(arr) {

  var newArr = [1, 2, 6];
  
   console.log(Math.max(newArr));
}

sumAll([1, 4]);

According to https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/max

The very first example shows an “…array1” being passed into the Math.max(); method. Help please.

Also when I read .reduce(); I find discrepancies on the parameters used to explain how this method functions in one example I find the parameter accumulator but on w3 I find the parameter total.

When I attempt to use the accumulator function in my .reduce(); method as explained here:

The arrow notation causes an error.

When I use the getSum function I receive a blank console. Two questions in one sorry.

I’ve edited your post for readability. When you enter a code block into the forum, precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

markdown_Forums

1 Like

You have actually identified your own error. MDN shows Math.max(...array1) and you are trying Math.max(newArr). Can you spot the difference (besides the names of the arrays)?

1 Like

Thank you for the help, I do see the difference visually but need clarity on why this type of variable cannot be passed functionally.

Math.max() does not accept an array as an argument. The example of Math.max(...array1) is making use of the spread operator.

1 Like

K, that’s clears that up, thanks. Any help on the other portion?

newArr.reduce( );

What is it about reduce() that confuses you? It would help if you could give specific examples of code that you tried and how it behaved differently than expected.

I’ll just figure it out; as you point out I’m confused so maybe I’m not qualified to ask questions here. Thanks for the help up to this point.

I wasn’t suggesting that you aren’t qualified to ask questions, I just don’t know how to explain something in a helpful way without knowing what it is that you need help with.

Ok, but I think i’m really confused, the more I read the less I can really articulate my issue. Still working on the code.

Okee dokee. Feel free to come back with any questions as they arise.

1 Like