Descending Order

Lee·2022년 6월 15일

Algorithm

목록 보기
16/92
post-thumbnail

❓ Descending Order

Q. 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.

Examples:
Input: 42145 Output: 54421

Input: 145263 Output: 654321

Input: 123456789 Output: 987654321

✔ Solution

function descendingOrder(n) {
  let lastN = n.toString().split("").sort().reverse().join("");
  return Number(lastN);
}

Check

profile
Lee

0개의 댓글