by Sonya Moisset

Three ways you can find the largest number in an array using JavaScript

In this article, I’m going to explain how to solve Free Code Camp’s “Return Largest Numbers in Arrayschallenge. This involves returning an array with the largest numbers from each of the sub arrays.

There are the three approaches I’ll cover:

  1. with a FOR loop
  2. using the reduce() method
  3. using Math.max()

The Algorithm Challenge Description

Return an array consisting of the largest number from each provided sub-array. For simplicity, the provided array will contain exactly 4 sub-arrays.
Remember, you can iterate through an array with a simple for loop, and access each member with array syntax arr[i].

Provided test cases

largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]) should return an array.
largestOfFour([[13, 27, 18, 26], [4, 5, 1, 3], [32, 35, 37, 39], [1000, 1001, 857, 1]]) should return [27,5,39,1001].
largestOfFour([[4, 9, 1, 3], [13, 35, 18, 26], [32, 35, 97, 39], [1000000, 1001, 857, 1]]) should return [9, 35, 97, 1000000].

Approach #1: Return the Largest Numbers in a Array With a For Loop

Here’s my solution, with embedded comments to help you understand it:

And here it is without my comments:

Approach #2: Return the Largest Numbers in a Array With Built-In Functions — with map() and reduce()

For this solution, you’ll use two methods: the Array.prototype.map() method and the Array.prototype.reduce() method.

  • The map() method creates a new array with the results of calling a provided function on every element in this array. Using map will call a provided callback function once for each element in an array, in order, and constructs a new array from the results.
  • The reduce() method applies a function against an accumulator and each value of the array to reduce it to a single value.

The ternary operator is the only JavaScript operator that takes three operands. This operator is used as a shortcut for the if statement.

(currentLargestNumber > previousLargestNumber) ? currentLargestNumber : previousLargestNumber;

This can also be read as:

if (currentLargestNumber > previousLargestNumber == true) {    return currentLargestNumber;} else {    return previousLargestNumber;}

Here’s my solution, with embedded comments:

And here it is without comments:

Approach #3: Return the Largest Numbers in a Array With Built-In Functions — with map() and apply()

For this solution, you’ll use two methods: the Array.prototype.map() method and the Function.prototype.apply() method.

  • The apply() method calls a function with a given this value and arguments provided as an array (or an array-like object).

You can pass an array of arguments to a function by using the apply() method and the function will execute the items in the array.

Such functions are known as variadic functions, and they can accept any number of arguments instead of a fixed one.

The Math.max() function returns the largest of zero or more numbers, and we can pass any number of arguments.

console.log(Math.max(4,5,1,3)); // logs 5

But you can’t pass an array of numbers to the method like this​:

var num = [4,5,1,3];console.log(Math.max(num)); // logs NaN

This is where the apply() method turns out to be useful:

var num = [4,5,1,3];console.log(Math.max.apply(null, num)); // logs 5

Note that the first argument to apply() sets the value of ‘this’, not used in this method, so you pass null.

Now that you have a method to return the largest number in a array, you can loop through each sub-arrays with the map() method and return all largest numbers.

Here’s my solution, with embedded comments:

And without comments:

I hope you found this helpful. This is part of my “How to Solve FCC Algorithms” series of articles on the Free Code Camp Algorithm Challenges, where I propose several solutions and explain step-by-step what happens under the hood.

Three ways to repeat a string in JavaScript
In this article, I’ll explain how to solve freeCodeCamp’s “Repeat a string repeat a string” challenge. This involves…medium.freecodecamp.comTwo ways to confirm the ending of a String in JavaScript
In this article, I’ll explain how to solve freeCodeCamp’s “Confirm the Ending” challenge.medium.freecodecamp.comThree Ways to Reverse a String in JavaScript
This article is based on Free Code Camp Basic Algorithm Scripting “Reverse a String”medium.freecodecamp.comThree Ways to Factorialize a Number in JavaScript
This article is based on Free Code Camp Basic Algorithm Scripting “Factorialize a Number”medium.freecodecamp.comTwo Ways to Check for Palindromes in JavaScript
This article is based on Free Code Camp Basic Algorithm Scripting “Check for Palindromes”.medium.freecodecamp.comThree Ways to Find the Longest Word in a String in JavaScript
This article is based on Free Code Camp Basic Algorithm Scripting “Find the Longest Word in a String”.medium.freecodecamp.comThree Ways to Title Case a Sentence in JavaScript
This article is based on Free Code Camp Basic Algorithm Scripting “Title Case a Sentence”.medium.freecodecamp.com

If you have your own solution or any suggestions, share them below in the comments.

Or you can follow me on Medium, Twitter, Github and LinkedIn, right after you click the green heart below ;-)

‪#‎StayCurious‬, ‪#‎KeepOnHacking‬ & ‪#‎MakeItHappen‬!

Additional Resources