descendingOrder.js (7kyu 07)

Codewars 알고리즘 풀이


Description

  • Your task is to make a function that can take any non-negative integer as a argument and return it with its digits in descending order.
    • 음수가 아닌 정수를 내림차순(descending order)으로 반환한다.



Solution 01

function descendingOrder(n) {
  return Number(n.toString().split('').sort((a, b) => b - a).join(''));
}

descendingOrder(0);          // 0
descendingOrder(1);          // 1
descendingOrder(1234);       // 4321
descendingOrder(4801);       // 8410
descendingOrder(123456789);  // 987654321


Solution 02

function descendingOrder(n) {
  return Number(String(n).split('').sort((a, b) => b - a).join(''));
}

descendingOrder(0);          // 0
descendingOrder(1);          // 1
descendingOrder(1234);       // 4321
descendingOrder(4801);       // 8410
descendingOrder(123456789);  // 987654321


Solution 03

function descendingOrder(n) {
  return +(('' + n).split('').sort((a, b) => b - a).join(''));
}

descendingOrder(0);          // 0
descendingOrder(1);          // 1
descendingOrder(1234);       // 4321
descendingOrder(4801);       // 8410
descendingOrder(123456789);  // 987654321