Result already correct, but lesson wont pass

Tell us what’s happening:
I already check that was already correct in console.log but the lesson didn’t give me a pass. Or I have wrong at something?

Your code so far


function largestOfFour(arr) {
let current = 0;
let result = [];
for (let i = 0; i < arr.length; i++){
  // console.log(arr[i]);
  for (let j = 0; j < arr[i].length; j++){
    if (current < arr[i][j]){
      current = arr[i][j];
    }
    // console.log(current);
    // console.log(arr[i][j]);
  }
  // console.log(current);
  if (arr[i].indexOf(current) >= 0){
    result = arr[i];
  }
}
console.log(result);
return result;
}

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 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36.

Challenge: Return Largest Numbers in Arrays

Link to the challenge:

Your solution assumes that the largest will be bigger than 0. What if all numbers are negative?

Also, you need the large number in each sub array, but you never reset current between arrays.

3 Likes

You’re

  1. Initialising current in the wrong place, in your code it’s going to be whatever the value was set at from the last sub array
  2. Not taking into account negative values
2 Likes

I didn’t think about this, thank you.

1 Like

Alright, ill check back, thank you.

1 Like

Im so sorry, that was a big wrong. First time i think that should be take the sub-array who contain a big number, but the real think is take every sub-sub-array who contain big number.

I completed, but i have a question. Here my code :

function largestOfFour(arr) {
  let result = [-99999,-99999,-99999,-99999];
  for (let i = 0; i < arr.length; i++){
    for (let j = 0; j < arr[i].length; j++){
      if (arr[i][j] > result[i]){
        result[i] = arr[i][j];
      }
    }
  }
  return result;
}

My question is : if i want make my initial result more dynamic (i mean without set the value) how to do that? What should i read about that?

Great question. What about setting the results array to contain a number from each subarray?