Alphabet to decimal to binary
by trentHarlem
HTML
<h2>Alphabet to Binary</h2>
<label for="alphaUserInput">Type a letter here:</label>
<input id="alphaUserInput" type="text" />
<br /><br />
<button id="alpha_button" type="submit">then Click this</button>
<p id="alpha_display"></p>
<!-- <br /><br /> -->
<h3>
Alphabet to Decimal to Binary
</h3>
<label for='userInput'>Type a Letter here:</label>
<input id='userInput' type='text'/>
<br><br>
<button id='button' type='submit'>
then Click this
</button>
<p id='display'>
</p>
JavaScript
const display = document.getElementById('display')
const userInput = document.getElementById('userInput')
const alphaUserInput = document.getElementById('alphaUserInput')
const button = document.getElementById('button')
const counterDisplay = document.getElementById('counter_display')
const counterDisplayTwo = document.getElementById('counter_display_two')
const alphaDisplay = document.getElementById('alpha_display')
const alphaButton = document.getElementById('alpha_button')
const counterStart = document.getElementById('counter_start')
const alpha = "0abcdefghijklmnopqrstuvwxyz"
function decimalToBinary(decimal) {
decimal = userInput.value || decimal
let binary = ''
while (decimal > 0) {
binary = (decimal % 2) + binary
decimal = Math.floor(decimal / 2)
}
display.innerHTML = binary.padStart(8, '0')
return binary.padStart(8, '0')
}
function binaryToDecimal(binary) {
let decimal = 0;
for (let i = binary.length - 1, j = 0; i >= 0; i--, j++) {
decimal += binary[i] * Math.pow(2, j);
}
return decimal;
}
button.addEventListener('click', decimalToBinary);
function alphaToBinary(letter) {
letter = alphaUserInput.value
console.log(letter, alpha.indexOf(letter))
let decimal = alpha.indexOf(letter)
let binary = decimalToBinary(decimal)
alphaDisplay.innerHTML = binary.padStart(8, '0')
return binary.padStart(8, '0')
}
alphaButton.addEventListener('click', alphaToBinary)