Algorithm Scripting - Return Largest Numbers in Arrays

Working through the algorithm scripting to create an array only with the largest numbers out of an array with subarrays. My code thus far below - appreciate any feedback/advise on whether my codes can work or not for the problem statement!

function largestOfFour(arr) {
  let largestNumberArray = [];
  Math.max(...arr.push(largestNumberArray));
  return largestNumberArray;
}

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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36

Challenge: Basic Algorithm Scripting - Return Largest Numbers in Arrays

Link to the challenge:

1 Like

Why don’t you try the code and let us know if you have any question about the result?

1 Like

Hey @jaydenkeh ! You are super close. Great use of the spread operator to find the max number in that array index. Consider the use of something that can iterate through the rest of the array because as it stands the code is missing a few instructions that will allow you to return the solution you want!

Any additional questions or want more tips then let me know :slight_smile:

Tom

2 Likes

don’t forget this given suggestion:
Remember, you can iterate through an array with a simple for loop, and access each member with array syntax arr[i].
but inside that you could break down the code into different lines for each step and console.log results to see what is happening.
e.g.

console.log(arr[i])
1 Like

Hi @jaydenkeh

Mi recomendation is you should take a break and back later to this challenge or even tomorrow. You will expend less time solving challenges if you give your brain time to figure out. You always can jump to the next challenge or do a different thing.

Anyway you are close to solve it and he is my hint : notice that you have a array of subarrays, and you need to find max number in every subarray. I think that for loops are beautiful this time of year. :wink:

1 Like

Thanks Tom. A quick one - as the code stands, I am only pushing the first subarray largest number am i right?

Hey @jaydenkeh Thank you for your patience :slight_smile: well as it stands, your pushing to …arr. Make sure you check out the syntax for .push here and make sure you’re pushing the highest number to the right array i.e. largestNumberArray.

In terms of the subarray that you’re accessing, think back to how we access arrays. These subarrays could be considered as elements like we access elements in a standard array. e.g.

  • arrElementExample = [6,3,7,8,11,65,12]

We can use Array indexing to access the following numbers with some examples below

  • arrElementExample[2]; // 7

  • arrElementExample[4]; //11

The same can be said for your array. Think of these subarrays in a similar way to the arrElementExample :slight_smile:

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

If you have any questions about the above or anything else then please let me know :slight_smile: Many thanks, Tom