Q:

JS: Return a number to a specified power of 10

I'm working on a problem that requires me to return a number rounded to a specified power of 10. For example: 
1234, specified power = 2 => 1200
1234, specified power = 3 => 1000

I did find a solution to the problem with this function:
const roundToPower = (num, pow) => {
    return Math.round(num / Math.pow(10, pow)) * Math.pow(10,pow)
};

However, I am not exactly sure how and why it works.
Is someone able to break it down for me? Thanks!

A:

Lets divide the upper function into three parts

num / Math.pow(10, pow)  will divide the given number by the power of 10 which is given. e.g for pow = 3 num will be divided by 1000
It then uses use Math.round() on that decimal.
Then again to equalize the division we did in first step it multiples again with power of ten Math.pow(10,pow)

Example
For pow = 3 and num = 1230
=> Math.round(1230 / Math.pow(10, 3)) * Math.pow(10, 3)
=> Math.round(1230 / 1000 )) * 1000
=> Math.round( 1.230 )) * 1000
=> 1 * 1000
=> 1000

