Return Largest Numbers in Arrays - please help

Tell us what’s happening:
please help me to figure out what is wrong

Your code so far


function largestOfFour(arr) {
  // You can do this!
  var max;
  var newArray = [];
  for (var i = 0; i > arr.length; i++){
    max = 0;
    for (var j = 0; j > arr.[j].length; j++){
      if (arr[i][j] > max)
      max = arr[i][j];
    }
    newArray.push(max);
  }
  return newArray;
}

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/76.0.3809.100 Safari/537.36.

Link to the challenge:

What do the failing tests say?

remember that a loop execute only if the condition is true. could i > arr.length ever be true?

Thanks for your answer. I did fix the i > arr.length for i < arr.length, and still not working? here is the error shown at the bottom

SyntaxError: unknown: Unexpected token (7:28)
5 | for (var i = 0; i < arr.length; i++){
6 | max = 0;

7 | for (var j = 0; j < arr.[j].length; j++){
| ^
8 | if (arr[i][j] > max)
9 | max = arr[i][j];
10 | }

The (7:28) means that the unexpected token is on line 7 at position 28. If you look in the editor, you will also see that the character (unexpected token) has a red squigly line under it.

it shows the bracket before J to be the problem:

7 | for (var j = 0; j < arr.[j].length; j++){
| ^

I really don’t know why that will be wrong?

What does the . before the [ do?

got it, i deleted the .

now it shows this error:
// running tests

Cannot read property ‘length’ of undefined

Cannot read property ‘length’ of undefined

Cannot read property ‘length’ of undefined

Cannot read property ‘length’ of undefined

// tests completed :frowning: I’m sorry, still not working. here is the whole code again:


function largestOfFour(arr) {
  // You can do this!
  var max;
  var newArray = [];
  for (var i = 0; i < arr.length; i++){
    max = 0;
    for (var j = 0; j < arr[j].length; j++){
      if (arr[i][j] > max)
      max = arr[i][j];
    }
    newArray.push(max);
  }
  return newArray;
}

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

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 easier to read.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.

markdown_Forums

Can you explain what this piece of your for statement does?

j < arr[j].length;

that is to go through the subarray

Look at the actual code. What does it do? What does it mean?

Hi Ariel

Thanks for trying to help me. I went back and started from scratch following other examples, and this code worked for that challenge:


var results = [];
  for (var n = 0; n < arr.length; n++) {
    var largestNumber = arr[n][0];
    for (var sb = 1; sb < arr[n].length; sb++) {
      if (arr[n][sb] > largestNumber) {
        largestNumber = arr[n][sb];
      }
    }

    results[n] = largestNumber;
  }

  return results;
}

Thanks for all your effort to help me. I really appreciate it!

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 easier to read.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.

markdown_Forums

as long as you understood why the last version of your code didn’t work, then awesome