Skip to content

Instantly share code, notes, and snippets.

@EliTheCoder
Last active November 30, 2018 16:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save EliTheCoder/6ae6c13e157e443d2b4582e1ce8c8249 to your computer and use it in GitHub Desktop.
Save EliTheCoder/6ae6c13e157e443d2b4582e1ce8c8249 to your computer and use it in GitHub Desktop.
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