Return Largest Numbers in Arrays - Feedback please :)

Return Largest Numbers in Arrays - Feedback please :)
0

#1

Hello everyone,

Need some feedback on this challenge. I am trying to pull the largest number from each sub-array. I know at the end I need a final array that pulls all the largest values. But in the meantime, I am just concatenating the largest values from each of the sub-arrays just to see the “If” statement makes sense. I noticed that the largest value out of all the sub-arrays will stop the concatenation when it’s in the middle of the array, leaving out the rest of the sub-arrays that come after it.

How do I pull all the largest values into the concatenation without breaking the iteration?
Thanks!

Code below:

function largestOfFour(arr) {

var a = 0;
var b = 0;
var total = 0;
var runningNumber = 0;
for (var i=0; i < arr.length; i++) {
for (var j=0; j < arr[i].length; j++) {
b = arr[i][j];

if (b > a) {

  a = b;
  
 total +=  "" + a ;
 
} // end of if statement

} //end of inner loop

} //end of outer for loop

return total ; 

}//end of function

largestOfFour([[3,2], [12,4], [5,6,7]]);


#2

I take it you want the result of the function above is the string “03127” ? If so, then this is what you should correct on your code (I put my comment in capslock):

function largestOfFour(arr) {

var a = 0;
var b = 0;
var total = 0;
var runningNumber = 0;
for (var i=0; i < arr.length; i++) {
	a = 0; //AS A COMPARISON VARIABLE, YOU HAVE TO REINITIALIZED a=0 OR ELSE THE 
           // VALUE OF a WILL BE FROM THE LAST ITERATION 
    for (var j=0; j < arr[i].length; j++) {
    	b = arr[i][j];
        if (b > a) {
          a = b; //DON'T FORGET, a CONTAINS TEMPORARY VALUE FOR HIGHEST VALUE
        }        // DON'T PUT IN ON THE TOTAL YET			
    } //end of inner loop
	total +=  "" + a ; //TOTAL SHOULD BE APPENDED IN THE END OF ITERATION
} //end of outer for loop

return total ;

}//end of function


#3

Thanks so much for the feedback ! Got it to work