Skip to content

Instantly share code, notes, and snippets.

@bryancodesjs
Created January 30, 2022 04:05
Show Gist options
  • Save bryancodesjs/583de6854160c7647441703bdc4ec5a0 to your computer and use it in GitHub Desktop.
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 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