Return Largest Numbers in Arrays JS

Return Largest Numbers in Arrays JS
0

#1

help? please TT3TT
what I’m doing wrong?

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

var cosa = [];
var max= 0;

for(var i = 0; i < arr.lenght; i++ ) {
max = 0;

for(j = 0; j < arr[i].length; j++){
  
  if(arr[i][j] > max){
    max=arr[i][j];   
  
  }
}

cosa.push(max);  

}
return cosa;
}

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


**Your browser information:**

User Agent is: <code>Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:62.0) Gecko/20100101 Firefox/62.0</code>.

**Link to the challenge:**
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-algorithm-scripting/return-largest-numbers-in-arrays

#2

You have one typo here:

for(var i = 0; i < arr.lenght; i++ ) {

An undeclared variable here:

for(j = 0; j < arr[i].length; j++){

When you will fix these problems it will fail only the last test, because you’re not considering the negative numbers in your logic ^^
When you state max = 0 and then if stuff > max it implies that if you have an array composed by only negative numbers your alghoritm will return 0, not the largest number ( because the largest number will be less than 0)

Good luck! :slight_smile:


#3

thank you so much! <3