Last active
November 30, 2018 16:46
-
-
Save EliTheCoder/6ae6c13e157e443d2b4582e1ce8c8249 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
let bint2bin = (bint) => { | |
let bintarr = bint.split(" "); // puts the string of numbers seperated by spaces into an array | |
let togg = false; // toggles whether the number means 0 or 1 (false is 0, true is 1) | |
let result = ""; // the string we will put all the binary numbers into | |
for (let i = 0; i < bintarr.length; i++) { | |
let str = ""; // string of 0s or 1s to add to result | |
for (let n = 0; n < parseInt(bintarr[i]); n++) { | |
if (togg) { | |
str += "1"; // adds a 1 to str | |
} else { | |
str += "0"; // adds a 0 to str | |
} | |
} | |
result += str; // adds str to the result | |
togg = !togg; // toggles the togg variable | |
} | |
return result; // outputs the result to the console | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment