-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdescending-order.js
More file actions
34 lines (23 loc) · 906 Bytes
/
descending-order.js
File metadata and controls
34 lines (23 loc) · 906 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
//7 Kyu
//Descending Order
//Fundamentals
// Your task is to make a function that can take any non-negative integer as an 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
//Solution I
function descendingOrder(n){
//store n as a string
let strNum = String(n)
//split string n into array of str nums. then sort the array in descending order. join it to a string num
let arrNums = strNum.split('').sort((a,b)=>b-a).join('')
//return number of that rearranged string num
return Number(arrNums)
}
//Paramaters
//int-> non-negative, wont be null, no funny bizz
//return
//integer -> of sorted nums, descending order (highest to lowest)
//examples
//console.log(descendingOrder(12345), 54321)