There is a number:

var chislo = 123456

How do you flip it – reflect it?

Solution

Convert the number to a string

chislo.toString()

Now we have a string data type. A string is an iterable object, so it can be cast to an array:

[...chislo.toString()]

Each “digit” now lies under a separate array element. Let’s reverse() this array

[...chislo.toString()].reverse()

Now we just need to glue all the individual lines into one

[...chislo.toString()].reverse().join("")

And now we have a string variant of the inverted number. Now all we have to do is to convert the string to a number:

Number([...chislo.toString()].reverse().join(""))

All done!


All commands in the browser console:

0 Shares:
Leave a Reply

Your email address will not be published. Required fields are marked *

You May Also Like