Return Largest Numbers in Arrays Error

Tell us what’s happening:

Your code so far


function largestOfFour(arr) {
  var result = [];
  for (var n  =0; n< arr.length; n++) {
    var Large = arr [n][0];
      for (var sb = 1; sb <arr[n].length; sb++) {
        if (result[n][sb] > Large ){
          Large = val[n][sb];
        }
      }
      result[n] = Large;
  }
  return val;
}
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/71.0.3578.98 Safari/537.36.

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

** Interesting!!!**
Code is looking the same as https://guide.freecodecamp.org/certifications/javascript-algorithms-and-data-structures/basic-algorithm-scripting/return-largest-numbers-in-arrays/ but have another variables and my code get error while that one is working… HOW AND WHY?

Hey, so firstly, well done on your solution, you’re almost there. Your issue seems to be that you are confusing variable name ie, result on line 6 in your if statement condition is an empty array, you want to change that to arr, val on line 7 within your if statement is supposed to be arr as well, and you want to return something other than the non existent val, I’ll let you deduce what you should return for the solution to work.

A few other tips for you: its best practice to not use capital letters at the start of your variable names, rather use ‘camelCase’, also try to pay a touch more attention to detail with the spaces you use, ie: arr [n][0] should look like arr[n][0] and try to keep the spaces when declaring your loops consistent. This will improve readability in your code and keep the OCD coders at bay.

Well, that all from me, good luck and keep coding, you’re getting there! :slightly_smiling_face:

The first thin I have noticed is that you are using the results array as a parameter in your for loop without ever actually pushing anything into it:

        if (result[n][sb] > Large ){
          
          Large = val[n][sb];
        }

The next thing I am noticing is that you have the same situation but using the variable val below it:

          Large = val[n][sb];

From what I can see val was never defined. Change both of these variables to arr

Finally, you are not returning the correct array, your return statement is returning a variable which does not exist. Your return statement should be:

return result;

Hope this helps.

var Large = arr [n][0];

You’ve got a space between arr and [: this causes the entire program to go into an infinite loop when I test it. You need to be a bit more careful typing, as stuff like this will cause very strange errors if it doesn’t error out immediately.

if (result[n][sb] > Large ){

This is not correct. You should be checking whether arr[n][sb] is greater than the current value of Large, you don’t care about result at this point. And as it is going to be an array with four numbers in, result[n][sb] doesn’t make sense.

Large = val[n][sb];

Again, not correct, I think you’ve copied something here. val is not a thing that exists in your program: you are using the variable arr for the array.