DEV Community

Cover image for codewar day3
G33kNoob
G33kNoob

Posted on

codewar day3

today the problem like this :
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. Essentially, rearrange the digits to create the highest possible number.

Examples:
Input: 42145 Output: 54421

Input: 145263 Output: 654321

Input: 123456789 Output: 987654321

then my solve :

const sortnumber = n => {
  let data = parseInt(
    n
      .toString()
      .split("")
      .sort()
      .reverse()
      .join()
      .replace(/,/g, "")
  );
  return data;
};
Enter fullscreen mode Exit fullscreen mode

i use some string, number and array prototype i learn a lot from developer.mozilla.org
i learning how the faster solve the problem, solve it with 'logic', then find how to cooding it :)
thanks codewar and mozilla developer

anyway other sorter solving like this

function descendingOrder(n){
  return +(n+'').split('').sort().reverse().join('')
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)