loading
open in
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
const largestNumber = (numArr) => {
 numArr.sort((a, b) => {
    if(a == b){
      return 0;
    }       
    a = a.toString().split('');
    b = b.toString().split('');     
    const maxLength = a.length > b.length ? a.length : b.length;        
    for(let i = 0; i < maxLength; i++){
      if(!a[i]){
        a[i] = a[i-1];
      }
      if(!b[i]){
        b[i] = b[i-1];
      }
      if(parseInt(a[i]) > parseInt(b[i])){
          return -1;
      }else if(parseInt(a[i]) < parseInt(b[i])){
          return 1; 
      }
    }
  });    
  return parseInt(numArr.join(''));
}

console.log(`Largest Number: ${largestNumber([50, 2, 1, 9, 5])}`); // 955021 - PASS
console.log(`Largest Number: ${largestNumber([50, 5, 56])}`); // 56550 - PASS
console.log(`Largest Number: ${largestNumber([420, 42, 423])}`); // 42342420 - FAIL (should be 42423420)
node v10.15.2 linux/amd64