Return the array containing the largest number challenge

Return the array containing the largest number challenge
0

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

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

What is the problem with the code ?


#2

Can you post the link to the challenge?


#3

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


#4
for(let i = 0; i < arr.length; i++){
    if(arr[i].includes(max)){
      return arr;
    }
  }

You canot return values like this from loops. Instead, push the value in an array and then return the array.


#5

You are doing it wrong, and got the challenge not correct.

Check the expected results, and challenge again.

I needs you find maximum value of each array , then put all these numbers together and return an array contains maximum number of each index.
e.g.

largestOfFour([[17, 23, 25, 12], [25, 7, 34, 48], [4, -10, 18, 21], [-72, -3, -17, -10]]) should return [25, 48, 21, -3].
If you check the expected result [25, 48, 21, -3], you see the first index(25) is the maximum number of first array, second(48) is the largest in second array and so on…

Hint: before you go to search for max value, have a result as array.

With two for loop(as you did) try to find the maximum number of each 2nd-level array(not the whole data).
TIP: always assume first element of each array as maximum.

No, this is possible to return arr just like the code you quoted, but OP got the challenge wrong.


#6

I dint really understand the question correctly . Thanks alot it was very helpful .