Skip to content

Instantly share code, notes, and snippets.

@Karlina-Bytes
Last active August 29, 2015 14:07
Show Gist options
  • Save Karlina-Bytes/bbd883e9b6c5b25032cc to your computer and use it in GitHub Desktop.
Save Karlina-Bytes/bbd883e9b6c5b25032cc to your computer and use it in GitHub Desktop.
For use with binToDecConvert.html. This JavaScript code responds to the button click.
/*********************************************************
* Responds to the "Convert!" button click.
* (This could be thought of as the "main" function).
* Step 1: Validates the text field input.
* Step 2: Determines which conversion to perform.
* Step 3: Performs the appropriate calculation.
* Step 4: Displays the result on the web page.
********************************************************/
function buttonClick() {
// Extract user data from the form fields.
var inputNumber = removeSpaces(document.getElementById("startField").value);
var inputBase = document.getElementById("startBaseSelect").value;
var outputBase = document.getElementById("endBaseSelect").value;
// Make sure that the given input is a valid number in the selected base.
if ( !validateInput(inputNumber, inputBase) )
displayResult("Error: Invalid number. Make sure the base is correct.");
// If the input is correct, perform the appropriate calculation.
else {
switch( inputBase ) {
// If input number is in binary ...
case "2":
if (outputBase == "2")
displayResult("Nothing changed (Binary to Binary).");
else
displayResult( "Binary Number " +
inputNumber +
" = Decimal Number " +
binaryToDecimal(inputNumber));
break;
// If input number is in decimal ...
case "10":
if (outputBase == "10")
displayResult( "Nothing changed (Decimal to Decimal)." );
else
displayResult( "Decimal Number " +
inputNumber +
" = Binary Number " +
decimalToBinary(inputNumber));
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment