Return Largest Numbers in Arrays helpp

Return Largest Numbers in Arrays helpp
0

#1

My browser hung when i ran the code,how is this an infinite loop.

Your code so far


function largestOfFour(arr) {
  // You can do this!
  var max = 0;
  var largest;
  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];
        largest = max;
        
      }
       
    }
    arr.push(largest);
  }
  return 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 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36.

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


#2

You’re pushing a value to arr inside a for-loop that iterates over arr. So arr.length keeps increasing at the same rate as i, so the for-loop won’t be able break.

Are you sure you want to push to arr? How about creating an empty array at the start of the function, then push values into it?


#3

okay i got what you said

function largestOfFour(arr) {
  // You can do this!
  var max = 0;
  var largest;
  var newarr = [];
  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];
        largest = max;

      }
       
    }
    newarr.push(largest);
  }
  return newarr;
}

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

it returns an array,it completes this challenge

largestOfFour([[4, 9, 1, 3], [13, 35, 18, 26], [32, 35, 97, 39], [1000000, 1001, 857, 1]]) should return [9, 35, 97, 1000000].

the rest of the challenges are the same as this and they arent being completed,i tried running them in ff as well but didnt work,is it my code or is there a bug


#4

Look at the max variable. Suppose you’ve already found the max value of the first array. Now when looking for the max of the second array, each of those numbers are being compared to the old max value. In other words the max of the previous arrays leak over to the next.

The input can also contain negative integers. If you had an input array that consists solely of negative integers (say, [-10, -25, -3, -4]), the function will incorrectly output 0, not -3


#5

i passed it but with a temporary solution,at the end of the outer loop before newarr.push(largest)
i set max to -100 but what if i meet up with a situation where numbers might go wayy below that?there is no point in leaving max undefined i will have to define it .


#6

You could use -Infinity as the initial max value. Any other numeric value will always be greater than that.


#7

Wont an infinity value cause the program to crash?Just like an infinity loop


#8

Infinity and -Infinity are valid numeric values in JS. There are cases where it’s meaningful to use them, like for looking for maximums or minimums.

Infinite loops are caused by loops that can’t break out of their looping condition (like in your code earlier).

Infinity has nothing to do with infinite loops


#9

okay,thanks for the help btw.