Return Largest Numbers in Arrays (Arrow Functions?)

Tell us what’s happening:

I want to know how to express the same code using Arrow Functions. I think that I should start learning this kind of coding because is more concise than the traditional, and I know that companies are looking for people who know how to write concise code.

Your code so far
function largestOfFour(arr) {
// You can do this!

‘create an Array’
let newArr=[];

‘search in each indexed array’
for (let i = 0; i<arr.length;i++){
let value = Math.max(…arr[i]);

return newArr;

largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);

**Your browser information:**

User Agent is: <code>Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36</code>.

**Link to the challenge:**

arrow functions are typically used as function arguments, typically also with higher order functions and there’s a particularly elegant way to use them here in combination with reduce and map

Have a think about how you can use map, reduce and arrow functions to solve this problem in one line - if you need any hints let me know

Yes, arrow functions are more concise. But they also come with other issues of their own. Namely that they don’t have a this binding.

So you won’t be able to use them everywhere depending on what you’re doing. This will become apparent when you get to the react section.

Converting to an arrow function is relatively straightforward

function someFunc() {
  return someValue

// single expression return
const someFunc = () => someValue

// multiple statements
const someFunc = () => {
 return someValue

Now I believe you mean you wish to write it using higher order functions, like with map, reduce, etc. This is what companies will look for, and what I will show.

Since you are iterating (looping) over an array, and then returning a new array of the same shape, you can use the map method.

// no arrow functions
function largestOfFour(arr) {
  return { return Math.max(...el)})

* only arrow functions
* this is a single expression, so 
* no curly braces are needed
const largestOfFour = (arr) => => Math.max(...el))

// you can even do a combination
function largestOfFour(arr) {
  return => return Math.max(...el))
1 Like

You are a Genius. I have tried the last one, its incredible just in one line…

function largestOfFour(arr) {

return> Math.max(…x))

thanks for your help I think that I have to read more about Higher-order functions.

My pleasure. I have this page bookmarked

On the left hand side you’ll see all the array higher order methods. Anytime you have an array, first look here.

But also, you can throw things into array literals so that you can work with these methods.

Like if you want to add an object to the end of an array

const obj = { b: 2 }
const arr = [ {a: 1} ]

const newArr = arr.concat([obj]) // [ {a: 1}, { b: 2 } ]

It might help if you think in terms of “how can I transform this data so I can work with it”. It’s how I approach problems.

Happy coding!