logo

Descending Order

Your task is to make a function that can take any non-negative integer as an argument and return it with its digits in descending order. Essentially, rearrange the digits to create the highest possible number.


My code:

function descendingOrder(n) {
                        let arrayNumber = n.toString().split('');
                        let sortedArray = arrayNumber.sort((a, b) => b - a); 
                        let highestNum = sortedArray.join('');
                        return parseInt(highestNum); 
                      }
                        
                    


And that's the best solution among all users:

function descendingOrder(n){
                        return parseInt(String(n).split('').sort().reverse().join(''))
                      }
                    

New things I learned today: