Created
January 30, 2022 04:05
-
-
Save bryancodesjs/583de6854160c7647441703bdc4ec5a0 to your computer and use it in GitHub Desktop.
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//this is an example integer | |
const myInt = 52132; | |
function descendingOrder(n) { | |
if (n > -1) { | |
let toArr = Array.from(String(n), Number); | |
let sorted = toArr.sort((a, b) => a - b).reverse(); | |
let joined = sorted.join(""); | |
return parseInt(joined); | |
} | |
} | |
descendingOrder(myInt); //this will return '53221' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment