Return Largest Numbers in Arrays

I feel like curling up in the corner to cry. I have been working on this bonfire for days!! And I was overcomplicating it. Part was I couldn’t figure out why I was unable to get to the nested arrays. Part was, it just can’t be that simple. But it really was.

I threw out four lines of code, added a variable and the thing worked like a charm!

 

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] .

If you are writing your own Chai.js tests, be sure to use a deep equal statement instead of an equal statement when comparing arrays.

Remember to use Read-Search-Ask if you get stuck. Write your own code.

function largestOfFour(arr) {

var maxArray = [];

for (i = 0; i < arr.length; i++){
var nestArray = arr[i];
large = Math.max.apply(Math, nestArray);

maxArray.push(large);
}

return maxArray;
}

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

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s