Return Largest Numbers in Array - Why Did This Work?

My solution is correct, but I thought I would need to include one more step where I use .push() to send the largest number from each subarray into it’s own array. Why isn’t .push() required in this case? I think it’s because of my use of .map(), but wanted to double check to make sure.

function largestOfFour(arr) { 
  let largest = arr.map( (a) => {
    a.reduce( (x,y) => {
      return largest = x > y ? x : y; 
    });
    return largest;   
  });
  return largest; 
}

Map makes the new array for you. This later challenge goes into more detail https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/functional-programming/implement-map-on-a-prototype

1 Like

I’ve edited your post for readability. 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 easier to read.

Note: Backticks are not single quotes.

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