Return Descending Number function

Return Descending Number function
0.0 0

#1

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);

#2

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 #';