Troubleshoot: Return Largest Numbers in Arrays

still the same issue… it mostly works only because the bigger number is accidentally at index 1 most of the time

array 1
0 > 0 false
1 > 0 true / bigPiece becomes 5
2 > 5 false
3 > 5 false

array 2
0 > 0 false
1 > 0 true / bigPiece is now 27
2 > 27 false
3 > 27 false

array 3
0 > 0 false
1 > 0 true / bigPiece is now 35
2 > 35 false
3 > 35 false

Thank you!! that is the little error I wasn’t seeing!!! Now I have to figure out the negative numbers.

for negative numbers you need to change this line

you have two possible ways:

  • use a number you have in the code
  • use the lowest value possible in javascript

That was what I was thinking.

Thank you so much for all the help!!

Caution spoiler ahead!!!

my solution that finally works:

  let bigPieces = [ ];
  let pieces = [ ];
  for (let i = 0; i < arr.length; i++){
     pieces = [...arr[i]];
     let bigValue = -5e324;
    for (let j = 0; j < pieces.length; j++){
      if (pieces[j] > bigValue){
      bigValue = pieces[j];
      }
    }
    bigPieces.push(bigValue);
  }
  return bigPieces;
}

and what happens if the biggest number in the array is -1000?
I know it is not in the tests, but try to make your algorithms more general then just for passing the tests

the two possible ways I listed above are totally general: the lowest possible value in javascript, or a number already present in the input

I edited my solution for the lowest negative number I could.Thanks again! :slight_smile:

Step by step instructions:

  1. Easiest is to convert whole array into string.
  2. Then split said string back to array.
  3. Go through said array to convert individual item back to number.
  4. Sort said array that now contain numbers in descending order.
  5. Take how many you like from begging of a array.

Spoiler ahead:


let arr = [[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]];
let str = ""+arr;
arr = (str.split(","));
arr = arr.map((item,index)=> Number(item));
arr.sort(function(a, b){return b - a});    
console.log(arr);

you could have searched on google… there is -Infinity

1 Like