DEV Community

Viren B
Viren B

Posted on • Edited on • Originally published at virenb.cc

FCC Algorithm Challenges / Return Largest Numbers in Arrays

Original post can also be found on my website, https://virenb.cc/fcc-005-largest-numbers

Return Largest Numbers in Array

function largestOfFour(arr) {
  return arr;
}

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



/// TESTS
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]

Above is the starter code provided for the challenge, "Return Largest Numbers in Arrays."

Our goal is to write a function that to take the input of a 2D array and return the largest number of each sub-array. The output format is to be an array. There are a few ways to iterate through an array. Let's think about this.

Thoughts

  • We are dealing with a 2-Dimensional array, meaning we will have to access values from an array within an array
  • Is the way to use two for loops?
  • Having some familiarity with arrays, we have some built in methods we can resort to
  • Since we have to take the largest number from each sub array, it would be helpful if the arrays were sorted from biggest to smallest
  • There is an array method, sort(), which would be helpful. There is a link below which shows how it is to be used. We need to provide a function to compare as sort's argument sort((a, b) => b - a)
  • Since our sub-array will be sorted in largest to smallest order, we can just do one loop and push the first index of each sub-array into the newArr empty array which was declared in the function already
  • Make sure to return newArr

Solution

Some Pseudocode

function largestOfFour(arr) {
  declare new empty array   

 loop through arr item (so arr.length)
    sort each sub array (so its in order from largest to smallest)  push first index (now largest) into new array (declared above)

 return new array
}

[SPOILER: SOLUTION TO CODE BELOW]

function largestOfFour(arr) {
  let newArr = [];

  for (let i = 0; i < arr.length; i++) {
    arr[i].sort((a, b) => b - a);
    newArr.push(arr[i][0]);
  };

  return newArr;
}

Method

  1. Read (!)

    • Read the instructions first. Make sure you understand what it being asked of you.
    • Read the starter code. Go line by line, just making sure you know what is going on initially.
    • Have a look at the tests. If the problem isn't clear to you, looking at the tests might give you an inclination of what kind of output you should aim for (i.e. instead of returning an array, maybe the problem is only asking for an index within the array).
  2. Think & Write

    Now that you've read through the instructions, starter code, and tests, it's time to analyze what to do and in what order. It may be handy to write out pseudocode.

  3. Code

    Once you've thought about what you'd like to do, and in what order, start to convert your pseudocode into JavaScript code.

There's been too many times where I've tried to jump write into writing the code without thinking it through (in projects and coding challenges). That will leave you testing it way too many times, creating unnecessary variables, and running into more problems then you need to handle. If I try to follow the above method, it leaves me with a more clear mind of what I'm doing and hopefully writing some DRY code.

Links & Resources

Return Largest Numbers in Array on FCC

FreeCodeCamp

Donate to FCC!

Solution on my GitHub

Array.prototype.sort() MDN Docs

Thank you for reading!

Top comments (0)