0

I have a scores array in my database. I want to extract maximum value from that array in my ejs webpage.

I am not sure but tried the following code:

<div>
       <%-Math.max(user.scores)%>
</div>

This not giving the desired result.

For eg if the scores array is [1, 4, 99, 2, 7], it should give me 99 on my webpage.

1 Answer 1

5

The javascript function Math.max() takes in a list of arguments and not a single argument aka an array.

Thus if you use the ES6 spread syntax for arrays it looks like this Math.max(...user.scores)

This will spread the elements of the array as arguments for the Math.max() function.

Here is the MDN documentation for the Math.max() function: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/max

Sign up to request clarification or add additional context in comments.

1 Comment

That's of great help. Thank you @phillip-schulze

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.