Last active
October 14, 2022 09:02
-
-
Save johnfoderaro/f960aafdaed087a1163c to your computer and use it in GitHub Desktop.
A simple function to convert a variable value to binary.
This file contains hidden or 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
function binaryConvert() { | |
var num = x; | |
if (num != Math.floor(num)) { | |
console.log("Please enter a number"); | |
} else if (num < 0) { | |
console.log("Please enter a positive number"); | |
} else { | |
var binary = parseInt(num, 10); | |
console.log(binary.toString(2)); | |
} | |
} | |
binaryConvert(); |
amazing job john
Pretty neat 👍
Great job
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
var num
.Math.floor
and then whether or not the variable is less than zero to ensure either zero or a positive value is entered.parseInt
to convert the variable from a string to an integer (if it was indeed entered as a string, in say a web form), followed bytoString(2)
to convert the variable to a binary value as a string.