Return Largest Numbers in Arrays SYNTAX HELP

Return Largest Numbers in Arrays SYNTAX HELP
0

#1

I’ve been working at this for a while. I know I’ve messed up the syntax but can’t find it. Can someone point out where I did this wrong?

Your code so far

function largestOfFour(arr) {
  // You can do this!
  var newArray = [];
  var placeHolder = 0;
  for (var i = 0; i < arr.length; i++) {
       for (var j =0; j < arr[i].length; j++) {
    if (arr[i][j] > placeHolder) {
      placeHolder = arr[i][j];
    }    
         }
     newArray.push(placeHolder);
       }
  
     return newArray; 
    
  
  

}

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

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:54.0) Gecko/20100101 Firefox/54.0.1 Waterfox/54.0.1.

Link to the challenge:


#2

I should point out that it gets some of the tests correct, but when the second of three tests is run, its seems to compare the results of the first two sub arrays and write the highest number from one of those arrays twice.


#3

hi,
You should declare your “placeholder” variable inside your first for loop.
The reason is when for i=1, the second loop starts,it compares with the previous value of placeholder and not with “0”.

function largestOfFour(arr) {
// You can do this!
var newArray = [];

for (var i = 0; i < arr.length; i++) {
var placeHolder = 0;
for (var j =0; j < arr[i].length; j++) {
if (arr[i][j] > placeHolder) {
placeHolder = arr[i][j];
}
}
newArray.push(placeHolder);
}

 return newArray; 

}

This should be your new function.


#4

Hey it worked! So glad you could help me. I would have never guessed that, but now I will keep in mind scope. I have been doing this for about a month or so and am terrible with syntax. Do you know of anywhere on the internet there may be a place for people who are awful at syntax, or is it just more practice that I need? (im thinking its the second one)


#5

Thats all my pleasure.
As far as i think,We can get better at syntax only while practicing.
One suggestion:
whenever you get stuck at any particular usecase(you were getting stuck for the second use case in your previous question),simply copy the parameters the usecase was passing and run ur code.
This way you will see the result and can identify whats wrong.
This is what i do.


#6

I will definietely try that next time. Thanks again