Easier solution for "Return Largest Number in Arrays"

There are tons of different solution all with thousands of if statements and variables all over the place at

But now with ES6 you can do an extremely easy one-lined solution:
const largestOfFour = arr => arr.map( subArr => Math.max(…subArr) );

I can’t find a way to reply to the challenge guide so I frustrativly create a topic.

and all the browser who aren’t compatible with ES6 should be called archaic :wink:

HI @sp4c3c0d3r !

Welcome to the forum!

In the early days of the forum, users were allowed to post their answers in the guide thread and discuss with other developers.

The moderators decided to close all of the discussion threads for the guide topics because it was just becoming a dumping ground for solutions without much discussion.

That’s true.
But, I don’t know if we really need to add a new solution to the guide because it is basically solution 3 from the guide without the arrow function.
Maybe one suggestion would be to just add it with solution 3 :woman_shrugging:

But the other moderators are free to chime and decide if it will be added to the guide.

Please read through these instructions on how to add contributions in the future.

Thanks!


Thank you, for your contribution. For future contributions, please wrap your solution within :

[details]
```
code goes here...
```
[/details]

Also, provide all of the necessary code to pass the challenge.

Also, when you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor ( </> ) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

I think this might be a time to clean up and reduce the solution set for this challenge. One with raw loops + one with ES6 syntax? I’m not big on the idea of changing the function signature just to make a one-liner though.

We could just keep solutions 1&3.

And then modify solution 3 to say this

function largestOfFour(arr) {
  return arr.map(Function.apply.bind(Math.max, null));
}

//using ES6 syntax
// const largestOfFour = arr => arr.map(subArr => Math.max(...subArr));
function largestOfFour(arr) {
  return arr.map(subArray => Math.max(...subArray));
}

I’m not a JS expert, but the above feels more idiomatic to me without changing the signature? Do people use apply.bind like that nowadays?

I personally haven’t, but I haven’t been doing this that long :laughing:

I agree, there should be a version that is just using spread instead of apply.bind.

I’m pretty sure I was about to change it at some point but didn’t. Maybe we should keep the apply.bind version just so it can be compared to the spread version.

Edit: although I’m not crazy about the wall of text the apply.bind version comes with. So maybe we should just get rid of it in favor of the spread version?

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.