zltzlt 发表于 2020-1-29 12:30:40

noobCode 01: SUPERSIZE ME.... or rather, this integer!

Write a function that rearranges an integer into its largest possible value.

superSize(123456); // 654321
superSize(105); // 510
superSize(12); // 21

If the argument passed through is single digit or is already the maximum possible integer, your function should simply return it.

function superSize(num){
    var a = num.toString().split("");
    a.sort(function(x, y) {if (parseInt(x) > parseInt(y)) {return 1;} return -1;});
    a.reverse()
    return parseInt(a.join(""))
}
页: [1]
查看完整版本: noobCode 01: SUPERSIZE ME.... or rather, this integer!