Return Largest Number of Array

Tell us what’s happening:
Even though the variables i and j has to be return 0,1,2,3
arr[i][j] outputs 0,1,2,3,4,5 and I couldn’t get why it is happening.

Your code so far

function largestOfFour(arr) {
let anArr=[];
for(let i=0; i<arr.length; i++){
  for(let j=0; j<arr[i].length; j++){
return console.log(anArr);

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 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36.

Challenge: Return Largest Numbers in Arrays

Link to the challenge:

One thing is that your return statement is incorrect. If you want to see what will be returned, either add console.log(anArr) before the return statement, or put whole function call inside of it.

Consider case when biggest number is not at the beginning of the array, i.e.: [32, 35, 37, 39], what your function will return in such case and why?

1 Like

the two loops are fine. But you are making the logic a bit more complicated.

  1. Define a variable ex. largest which will hold the largest value in each iteration. and will be added to the new array every time we find the largest value in each array,

  2. Initially largest is the first value of each array so lets compare and replace it. something like this
    if(current value > largest) then update the largest with the current value.
    This way you will find the largest value of that array

  3. then add the largest value to the new array on each outer loop interation.
    rest is upto you. Take some time to understand.

[and don’t return a console.log statement; just return ;]