Return Largest Numbers in Arrays: Can't figure out the error: Passing only 2 test cases

Tell us what’s happening:

Your code so far

function largestOfFour(arr) {
  // You can do this!
  var newArr=[];
  var largest=1;
  for(var i=0;i<arr.length;i++){
    for(var j=0;j<largest;j++){
       var largest=arr[i][j];
 return newArr;

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 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36.

Link to the challenge:

Your code will not work if the largest number in one of the arrays is not larger than the largest number in the array before it.

1 Like
var largest=arr[i][j];

you are trying to create another variable here instead of assigning the defined variable to something new. The above code should be written as such.

largest = arr[i][j]

The code you have written won’t actually pass the test. As of now, it will push a value that is simply larger than the previous value as you loop through the array of arrays. You want to return the single largest value from each of the array in the array.

1 Like

First, don’t use var, use const or let , depending about if your variable will change its value.
Go see MDN documentation

Second, var largest=1; stop the for loop to early. You have to do like in your first loop like so for(let j=0;j<arr[i][j].length;j++)

But at my opinion you should use .map() function instead of for loop :slight_smile: It’s just a liltle tricky at the beginning but very rewarding at the end I promise !

1 Like

Tysm :slight_smile: . Got the mistake.
@Nabi Tried using .map() function, took time but worked! Thanks!