Return Largest Numbers in Arrays understand for and .map

I was trying to answer without help, but my solution has a mistake.

I am trying to substitute one for by .map and make a for inside . map, my solution is wrong?why?
I see that .map return arrays multiple times.

function largestOfFour(arr) {
  // You can do this!

  var results = arr.map(function(num) {
var largestNumber = 0;
for(i=0; num.length; i++) {
  if (arr[i] > arr[largestNumber]) {
    largestNumber = arr[i];
  }
}
return largestNumber;
  });

  return results;
}

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

You have a lot of typos:

function largestOfFour(arr) {
  // You can do this!

  var results = arr.map(function(num) {
    var largestNumber = 0;
    for (let i = 0; i < num.length; i++) {  // <-- here you were missing comparison
      if (num[i] > largestNumber) {  // <-- here you were using arr instead of num and comparing to what?
        largestNumber = num[i]; // <-- same here
      }
    }
    return largestNumber;
  });

  return results;
}

A few issues here, but your approach is feasible.

  1. Declare your variables outside your loop.

  2. arr is an array of arrays. That means when you map it, ‘num’ will be the array you are working with, not ‘arr’.

  3. Your for loop should be:
    for(var i = 0; i < num.length; i++)

  4. Your comparison is way off, what you need is:

if(num[i] > largestNumber) { largestNumber = num[i]; }

Thank you very much.
I did the correct the loop and the name of variables.
I am trying to solve without consulting the internet, so I forget some part of code.
i am training for interview.

Thanks, I don’t know what happen I forgot a important part of loop, I saw the error infinite loop, but I didn’t saw it was missing variable > variable.length.

I saw an answer with two loops, but I wanna to reduce the code. I always make confusion with arr[i] and arr[i][j], I don’t know why.

Thank you, I don’t know sometimes I make silly errors.

Yes, i was testing my memory, but I think I will have to write everyday.

Sorry, I stopped study for a while.
Not well, looking for another opportunities.