Help:Return Largest Numbers in Arrays

l don’t know what’s wrong with my code , it passed two tests and failed the two more .

function largestOfFour(arr) {
var largest =0;
var new_arr =[];
for (var i=0;i<arr.length;i++){
  for(var x=0;x<arr.length;x++){
    if (largest < arr[i][x]){
      largest = arr[i][x];
    }
  }
  new_arr.push(largest);
}
console.log(new_arr);
}

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 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36

Challenge: Return Largest Numbers in Arrays

Link to the challenge:

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

var largest =0;
//this line won't work for several reasons
//1. The highest number can be less than 0

//2. It is created outside the loop so it would save values 
//from any array and save them pass the new iteration which you do not want

//3. You currently are not returning a value

The naming convention you are using is not correct for JavaScript so instead of new_arr you would have newArr

ok thanks it is working now

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.