Check if a number is a power of another number | Returns true if y is a power of x ; The only power of 1 is 1 itself ; Repeatedly compute power of x ; Check if power of x becomes y ; check the result for true / false and print .
function isPower ( x , y ) { if ( x == 1 ) return ( y == 1 ) ; let pow = 1 ; while ( pow < y ) pow = pow * x ; return ( pow == y ) ; } document . write ( ( isPower ( 10 , 1 ) ? 1 : 0 ) + " " ) ; document . write ( ( isPower ( 1 , 20 ) ? 1 : 0 ) + " " ) ; document . write ( ( isPower ( 2 , 128 ) ? 1 : 0 ) + " " ) ; document . write ( ( isPower ( 2 , 30 ) ? 1 : 0 ) + " " ) ;