Advertisement
AlexanderHristov

Decimal to Binary converter

Dec 20th, 2018
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function decimalToBinary(inputNum) {
  2.  
  3.     let binary = [];
  4.  
  5.     while (inputNum > 0) {
  6.  
  7.         if (inputNum % 2 === 1) {
  8.  
  9.             binary.splice(0,0,1);
  10.             inputNum = (inputNum - 1) / 2;
  11.  
  12.         } else {
  13.  
  14.             binary.splice(0,0,0);
  15.             inputNum /= 2;
  16.         }
  17.     }
  18.  
  19.     binary = binary.join('');
  20.     console.log(binary);
  21. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement