#Bonfire: Return Largest Numbers in Arrays
Explore tagged Tumblr posts
Text
Bonfire: Return Largest Numbers in Arrays
Difficulty Level: 1 lighter scrape, you pathetic fake
My Solution:
function largestOfFour(arr) { // You can do this! var num = 0; for (var i = 0; i <arr.length; i++) { var temp = arr[i]; for (var j = 0; j<temp.length;j++) { if (temp[j] > num) {num=temp[j];} } arr[i] = num; } return arr; }
First, use two for loops, one iterating through your main array, then one to iterate through sub-arrays. In your encapsulated for loop, set up a temp variable for both your highest number and references to the arrays so that you can make an if comparison that will return the highest number as an integer back into your original array. Rereading this, it was a mistake to declare the var num = 0 globally, in that if the arrays fed in had not been arranged to increase in value in each subsequent array, the num variable would have remained at the maximum of the highest array.
Longing to see the new JS, HTML and CSS stuff added next week (?) or w/e. hope it is less repetitive and sophomorish “basics of CS and OOP”-tier
0 notes