Coding/Blog challenge 4

Challenge

Todays challenge was brought to me by James Q Quick . image.png

Technique

Just mulling this problem over for a second, I automatically thought of the Array sort method. This line of thought means that I will need to turn this number into an array of numbers. Each number will have to be split apart as its own number. Okay, so in order to use the String split method we first have to convert this from an int datatype to a string datatype, then we gotta split them into an array of strings.

So let's just start there: Set our function & make our conversions.

function descendingOrder(numbers) {
    // convert datatypes and split into individual values
    let newOrder = numbers.toString().split('');
    return;
}

Now we can sort them, but sort will go in ascending order, and we want the opposite, right? Luckily, there is an Array method called reverse which will do exactly what it sounds like it will do, it will reverse. After that, we need to join and convert this back to an int data type.

function descendingOrder(numbers) {
    let newOrder = numbers.toString().split('');
    //set new variable equal to sorted, revesed array. Join these back together
    //replace all instances of a comma with an empty string
    newOrder = newOrder.sort().reverse().join('');
    return;
}

Finally, we need to return and integer, not a string. We can use the Number object to convert our string of integers into just integers. We can do that in the return statement.

function descendingOrder(numbers) {
    let newOrder = numbers.toString().split('');
    newOrder = newOrder.sort().reverse().join().replace(/,/g, "");
//convert string into numbers to return
    return Number(newOrder);
}

Bazinga!