How to Find the Largest Number in an Array Javascript

Return the largest numbers in arrays in JavaScript

Dylan Attal

This is a good challenge to test your understanding of bracket notation. In order to find the solution, we're going to have to iterate through a two-dimensional array. Let's do it!

Algorithm instructions

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].
  • largestOfFour([[17, 23, 25, 12], [25, 7, 34, 48], [4, -10, 18, 21], [-72, -3, -17, -10]]) should return [25, 48, 21, -3].

Solution #1: Math.max( ) & for loop

PEDAC

Understanding the Problem: Our input here is an array of arrays. Our output is an array. Ultimately, we need to pluck the largest number from each group of numbers given to us and return those largest numbers in an array.

Examples/Test Cases: Our prov i ded test cases are pretty straightforward. They indicate that negative numbers behave as we would expect them to. They also indicate that our output array should not be sorted ascending or descending, but rather should contain numbers in order by which array they were picked. So the largest number from group one goes first, the largest number from group two goes second, etc. That's nice for us; it's less work.

Data Structure: We are given an array of arrays as our input. The good people at freeCodeCamp have given us a hint that we can access the element of an array in bracket form like so: array = [2, 5, 7, 9] array[1] = 5.

Another useful piece of information is that we can access whole arrays as elements this way. Think about it: if our input is

            arr = [[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]          

then each array here is a single element of the parent array. Therefore

            arr[0] = [4, 5, 1, 3]          

This will be a useful way for us to access each group of numbers passed to us.

Algorithm:

  1. Go through each group of numbers in the input.
  2. Pick out the largest number in each group and store it.
  3. Return the group of largest numbers.

Code: See below!

Without comments:

Solution #2: Math.max( ) & .forEach( )

PEDAC

Understanding the Problem: Our input here is an array of arrays. Our output is an array. Ultimately, we need to pluck the largest number from each group of numbers given to us and return those largest numbers in an array.

This approach is similar to Solution #1, but this time we'll use .forEach() instead of a for loop to iterate through the input.

Examples/Test Cases: Our provided test cases are pretty straightforward. They indicate that negative numbers behave as we would expect them to. They also indicate that our output array should not be sorted ascending or descending, but rather should contain numbers in order by which array they were picked. So the largest number from group one goes first, the largest number from group two goes second, etc. That's nice for us; it's less work.

Data Structure: We are given an array of arrays as our input. When using .forEach() we won't have to worry about using bracket notation to access each array in the input. Instead, we'll be passing .forEach() a callback function to manipulate each element — each array — of the input.

Algorithm:

  1. Go through each group of numbers in the input.
  2. Pick out the largest number in each group and store it.
  3. Return the group of largest numbers.

Code: See below!

Without comments:

If you have other solutions and/or suggestions, please share in the comments!

How to Find the Largest Number in an Array Javascript

Source: https://medium.com/@DylanAttal/return-largest-numbers-in-arrays-in-javascript-9bbc43c7b264

0 Response to "How to Find the Largest Number in an Array Javascript"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel