I am a newbie and Im trying to practice codes with codewars. The problem is I already solve the problem, but it wont accept my codes.
Here's the problem:
Here's my solution:
function descendingOrder(n){
var result =(n.toString().split('').reverse().sort(function(a, b){return b-a}).join(''));
parseFloat(result);
return result;
}
console.log(descendingOrder(12345));
What I tried is of course removing the parameter in function which is 12345 since codewars has already done it.
CodePudding user response:
You have some unnecessary functionality (like reverse()
) in your code. In addition I understood you're dealing with integers, not floats:
const descendingOrder = (n) => {
const result = n.toString().split('').sort((a, b) => b - a).join('');
return parseInt(result);
};
console.log(descendingOrder(123454821));
CodePudding user response:
You could sort by default sorting and reverse the array, like
return value
.toString()
.split('')
.sort() // sort by string asc
.reverse()
.join('');
or sort with a callback descending and omit reversing.
return value
.toString()
.split('')
.sort((a, b) => b - a)
.join('');