r/JavaScriptTips • • Jun 14 '21

Create your own JavaScript function that can return the maximum value from a list of given numbers. Try following example.

Post image
10 Upvotes

2 comments sorted by

2

u/prof3ssorSt3v3 Jun 15 '21

Or... use the built-in Array reduce method.

let max = numbers.reduce((max, curr)=>{
  return curr > max?curr:max;
}, -Infinity);

1

u/backtickbot Jun 15 '21

Fixed formatting.

Hello, prof3ssorSt3v3: code blocks using triple backticks (```) don't work on all versions of Reddit!

Some users see this / this instead.

To fix this, indent every line with 4 spaces instead.

FAQ

You can opt out by replying with backtickopt6 to this comment.