fork download
  1. // Nazar Abdul Fattah
  2. // Suitmedia - Backend Developer Test
  3.  
  4. function hitungNomorBit(angka, nomorBit) {
  5. if (nomorBit !== 0 && nomorBit !== 1) return null;
  6. if (angka === 0) return nomorBit === 0 ? 1 : 0;
  7.  
  8. let count = 0;
  9. while (angka > 0) {
  10. if ((angka % 2) === nomorBit) count++;
  11. angka = Math.floor(angka / 2);
  12. }
  13.  
  14. return count;
  15. }
  16.  
  17. print(hitungNomorBit(13, 0)); // Output: 1
  18. print(hitungNomorBit(13, 1)); // Output: 3
  19. print(hitungNomorBit(13, 2)); // Output: null
  20.  
Success #stdin #stdout 0.02s 18568KB
stdin
Standard input is empty
stdout
1
3
null