Created
March 4, 2020 13:54
-
-
Save Risyandi/2b7fbdcbf9d234656980730cdebd7361 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* created by : risyandi @2020 | |
*/ | |
function countBits(num, bitNum) { | |
let binary = Number(num).toString(2); | |
let temp = ""; | |
let result = null; | |
for (let index = 0; index < binary.length; index++) { | |
let binNum = binary[index]; | |
if (binNum == bitNum) { | |
temp += binNum; | |
} else if (binNum > 2) { | |
return null; | |
} | |
} | |
result = parseInt(temp.length); | |
console.log(result); | |
return result; | |
} | |
countBits(13, 1); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment