Return Largest Numbers in Arrays- code isn't working

Tell us what’s happening:
I know this code isn’t very efficient, but I just can’t figure out why isn’t it working.

Your code so far


function largestOfFour(arr) {
  // You can do this!
  var big1=0,big2=0,big3=0,big4=0;
  for (var i = 0; i<arr.length; i++) {
    switch(i) {
      case 0:
    for (var j =0; j<arr[i].length;j++) {
      if(j=0) {
        if(arr[i][j] > big1) {
          big1 = arr[i][j];
        }
      }
     else if (arr[i][j]> arr[i][j-1]) {
       if(arr[i][j]> big1) {
         big1 = arr[i][j];
       }
     }
    }
    break;
    case 1:
     for (var j =0; j<arr[i].length;j++) {
      if(j=0) {
        if(arr[i][j] > big2) {
          big2 = arr[i][j];
        }
      }
     else if (arr[i][j]> arr[i][j-1]) {
       if(arr[i][j]> big2) {
         big2 = arr[i][j];
       }
     }
    }
    break;

    case 2:
        for (var j =0; j<arr[i].length;j++) {
      if(j=0) {
        if(arr[i][j] > big3) {
          big3 = arr[i][j];
        }
      }
     else if (arr[i][j]> arr[i][j-1]) {
       if(arr[i][j]> big3) {
         big3 = arr[i][j];
       }
     }else {
      big3 += 0
    }
    } 
    break;
   case 3:
        for (var j =0; j<arr[i].length;j++) {
      if(j=0) {
        if(arr[i][j] > big4) {
          big4 = arr[i][j];
        }
      }
     else if (arr[i][j]> arr[i][j-1]) {
       if(arr[i][j]> big4) {
         big4 = arr[i][j];
       }
     }
    }
    break;

    }
  }
console.log(big3);
  var biggest=0;
  let bigArr=[big1, big2, big3, big4];
  console.log(bigArr);
  for (var g =0; g<bigArr.length;g++) {
    if(g=0) {
      if(bigArr[g]> biggest) {
        biggest = bigArr[g];
      }
    }
     else if (bigArr[g]> bigArr[g-1]) {
       if(bigArr[g]> biggest) {
         biggest = bigArr[g];
       }
     }
    }
    console.log(biggest);

  return arr[biggest.split("")[3]];
}

console.log(largestOfFour([[13, 27, 18, 26], [4, 5, 1, 3], [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/75.0.3770.100 Safari/537.36.

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

what do the failing tests say?

// running tests

biggest.split is not a function

biggest.split is not a function

biggest.split is not a function

biggest.split is not a function

// tests completed

So, split() is a string method, but it seems you are using it on a number, you can’t do that as there is no number method called split()

Okay thanks for that. But what about the sorting I have done above. When I console.log them I can see they aren’t working either.

You’re using the assignment operator = in your conditional expressions instead of the equality operators == or ===.

As in:


if(j=0) {
        if(arr[i][j] > big1) {
          big1 = arr[i][j];
        }

It should be if (j == 0) or if (j === 0).

That’s the first problem I found, though as @ilenia pointed out, using the String.split() method on a numeric value is also an issue.

How did I miss that!
Thank you very much!

1 Like

@pratikjaiswal1998
No problem, glad to be able to help :slightly_smiling_face:

do you have a question about your code?

otherwise, please don’t post full working solutions. and if you have a question on a full working solution, please surround it with [spoiler] tags

1 Like

I don’t have questions, only answers.

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

If you want to compare your solution to others, use the Get a hint button on the challenge and there are alternative solutions you can compare yours to. Also, you can probably search older posts using the forum search feature or google the challenge name and find more there.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

Thank you for understanding.

1 Like