Return Largest Numbers in Arrays understand for and .map

Return Largest Numbers in Arrays understand for and .map
0.0 0

#1

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]]);

#2

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;
}

#3

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]; }


#4

Well you have at least 3 problems:

1st) Your for loop is in an infinite loop because you do not give it an exit condition (the 2nd of the three). You are using:

for(i=0; num.length; i++)

and it should look more like:

for(i=0; i < num.length; i++)

2nd) You probably need to review how the map function works, because you have passed a variable called num an anonymous function but then never use it. num contains each subarray of numbers. Before trying to use map, just try to solve the challenge with for loops. Once you have a working solution using for loops, then try using a high order function such as map, filter, or reduce.

3rd) You need to take a look at the following line of code and ask yourself what is it trying to check. I would suggest using a console.log(arr[i], arr[largestNumber]); right before this statement so you can see the values you are getting ready to compare. It will probably give you some insight as two what you really need to be comparing.

if (arr[i] > arr[largestNumber]) {

Keep trying. If you need any more help or do not understand anything I have said, please reply back and I will attempt to give you some more hints. We are all in this together!

EDIT Well I was too late to answer as two others already answered before me. I try not to give the complete solution, because I think it is more important for you to come up with the answer without seeing the completed solution. Oh well, I should have typed faster.


#5

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.


#6

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.


#7

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


#8

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


#9

How did the interview go?


#10

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