Return Largest Numbers in Arrays, help

Tell us what’s happening:

here I have a program that splices away the smallest number in the subarray, leaving only the biggest number.

when I console.log the examples, I was able to achieve the results, but still wouldn’t pass the test.

Your code so far


function largestOfFour(arr) {
  // You can do this!
  for (let i =0; i < arr.length; i++) {
    while (arr[i].length != 1) {
      if (arr[i][0] >= arr[i][1]) {
        arr[i].splice(1,1);
      } else {
        arr[i].splice(0,1);
      }
    }
  } 
  
  return arr;
}

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

I thought maybe its still in a nested array like [[4,27,39, 1001]]

so I tried

arr = arr[0].splice(0,4)

and

arr = [...arr[0]]

both didn’t work

what am I doing wrong here?

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.0 Safari/605.1.15.

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

First of all, it’s bad practice to mutate the array that’s supplied to you. You are better off making a copy and making all the operations with the copy. You can do that by using a spread operator or slice array method.

Anyway, back to your problem. Avoid using FCC challenge pages for debugging. Use something like repl.it or google chrome console instead, because they are much better suited for debugging. If you were to use a proper debugger you’d see that console.log() in your situation prints:

console.log(arr) // [ [ 5 ], [ 27 ], [ 39 ], [ 1001 ] ]

As you can see this is still an array of arrays ( each one has one element in it), and what you want is an array of numbers, that is why your tests are failing. In order to transform elements of an array into something else you can use a really handy array method called map. Think about how you can use map to solve your problem, read the MDN docs on map. Ask if you have more questions, good luck!