Trying to make a function that gives me the descending order version of a number backwards

so input: 123

output: 321

```
function descendingOrder(n)
{
var descending;
n = n.toString()
console.log(n)
if (n>0)
{
for(i=n.length ; n=0 ; n--)
{
descending+=n.charAt(i)
}
return parseInt(descending);
}
else
{
return console.log("not a positive integer");
}
}
descendingOrder(123);
```

Just to make sure I understand the output expectations, would and input of 193 need to output 931? Posting a few more tests cases would be helpful.

One problem with your existing code is the line:

```
for(i=n.length ; n=0 ; n--)
```

The 2nd part of the for loop should be a comparison and not an assignment. If the evaluation of this comparison is true, the for loop will continue. Also, do you really want to decrement n (i.e. nā) or i?

If my assumption about 193 being 931 is correct, then the following could work:

```
const descendingOrder = n => n > 0 ? Number([...''+n].sort((a,b) => b-a).join('')) : 'non-positive #';
```

1 Like