Return Largest Numbers in Arrays - Array only has 1 number

Return Largest Numbers in Arrays - Array only has 1 number
0

#1

Tell us what’s happening:

My solution finds the largest numbers in the array by eliminating all but the largest in each sub-array. The problem is that the numbers are still in sub arrays, like this:
[[27],[5],[39],[1001]]

The working solution requires a single array with 4 numbers like this:
[27, 5, 19, 1001]

To achieve this, I’ve created a blank array (largestArray) so that each remaining number is sliced in. However, only the final number is added (1001, in this case).
I suspected that the while loop was running several times before the second ‘if’ ran once, meaning that i==3 before a value was sliced, so I tried rewriting the code, seen below as ALT CODE, with the same problem.
Since ‘i’ is only incremented when a value is pushed, it leads me to believe that the problem is not the loop, but the method of adding a value to the array (slice). Now I believe that the slice function is overwriting each value so that only the most recent remains. However, using methods like ‘push’ or even ‘unshift’ (unsuitable anyway) return ‘1’, not even [1], and I have no idea why.

Short version: Only the value from the final sub-array is added to a new array, suspect slicing is the cause. All thoughts welcome.

Your code so far


function largestOfFour(arr) {

var largestArray = {};  
  
  for(var i = 0; i < arr.length; i++){
    while(arr[i].length > 1){
      if(arr[i][0] < arr[i][1]) {
        arr[i].splice(0, 1);
      }
      else{
        arr[i].splice(1, 1);
      }
    } 
    if(arr[i].length == 1) {
       largestArray = arr[i].slice();
    }
  }
  return largestArray;
}

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

ALT CODE:

function largestOfFour(arr) {

var largestArray = {};  
var i = 0;  

    while(i < arr.length){
      if(arr[i][0] < arr[i][1] && arr[i].length > 1) {
        arr[i].splice(0, 1);
      }
      else if (arr[i][0] > arr[i][1] && arr[i].length > 1){
        arr[i].splice(1, 1);
      }
      else if(arr[i].length == 1) {
       largestArray = arr[i].slice();
       i++;
    }
  }
  return largestArray;
}

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

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36.

Link to the challenge:


#2

You’re assigning the result of arr[i].slice() to largestArray for each loop. The last arr[i].slice() call (when i id 3, or [1001]) is the last value that is assigned to largestArray, and that’s what’s returned at the end.

You can change the initial value of largestArray to [], then instead of assigning, push the value to it. It still won’t give you the correct answer, but with a few more changes it should give the correct answer.


Though it can work, I won’t recommend using splice for this problem, because it mutates the input array. You need to get the maximum value from each subarray. There are simpler ways to do that.


#3

Many thanks! It’s good to know where the problem is coming from. The array has input now and it seems that my method is the biggest issue.