Created
June 17, 2015 19:58
-
-
Save teckliew/10fd4ca055c73572b8a7 to your computer and use it in GitHub Desktop.
Make a function that can take any non-negative integer as a argument and return it with it's digits in descending order.
This file contains hidden or 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
//my example | |
function descendingOrder(n){ | |
var digits = (""+n).split("").sort(function(a, b){return b-a}); | |
digits = digits.join(""); | |
return Number(digits); | |
} | |
//good examples | |
//1 | |
function descendingOrder(n){ | |
return parseInt(String(n).split('').sort().reverse().join('')) | |
} | |
//2 | |
function descendingOrder(n){ | |
return +(n + '').split('').sort(function(a,b){ return b - a }).join(''); | |
} | |
good
good
good
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
function descendingOrder(n){
return Number(n.toString().split('').sort((a,b)=> b-a).join(''));
}