Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

isPowerOfTow

const isPowerOfTow =(num)=>{

    if(num === 0){
        return false
    }
   while(num !==1){
        num /=2
    
        if(num %2 !==0 && num !== 1){
            return false
        } 
        
    }
    return true
    
    }

    const isPowerOfTowbitwise =(num)=>{
        return num && !(num&(num-1))

    }

    const powerOfTowRec =(num )=>{

      if(num ===0){
          return false
      }

    
      if(num %2 !== 0 && num !== 1){
          return false

      }
      if(num === 1){
          return true
      }


      return powerOfTowRec(num/2)

    }
    
    const nums=[0,1,2,3,4,6,8,9,256,1024]
    
    nums.forEach( num =>{
        console.log(num,powerOfTowRec(num))
    })
    
Source by www.hackerearth.com #
 
PREVIOUS NEXT
Tagged: #isPowerOfTow
ADD COMMENT
Topic
Name
8+9 =