Hey guys I'm totally new here and need help on an assignment. Last week we created a simple JS program that converts decimal to binary (without using parseint). The new assignment is to prompt the user for a decimal number, then display the resulting binary number in 8 separate div elements.
Although there might have been a more succinct way to do the conversion, here is what I did within javascript. For clarification I named the variables with respect to their corresponding base two power (2^7 place, 2^6 place, etc)
var seven = 0;
var six = 0;
var five = 0;
var four = 0;
var three = 0;
var two = 0;
var one = 0;
var zero = 0;
var input = document.getElementById("dec").value;
if (input < 128) {
seven = 0;
}
if (input > 128) {
input = input % 128;
seven = 1;
}
if (input < 64) {
six = 0;
}
if (input > 64) {
input = input % 64;
six = 1;
}
if (input < 32) {
five = 0;
}
if (input > 32) {
input = input % 32;
five = 1;
}
if (input < 16) {
four = 0;
}
if (input > 16) {
input = input % 16;
four = 1;
}
if (input < 8) {
three = 0;
}
if (input > 8) {
input = input % 8;
three = 1;
}
if (input < 4) {
two = 0;
}
if (input > 4) {
input = input % 4;
two = 1;
}
if (input < 2) {
one = 0;
}
if (input > 2) {
input = input % 2;
one = 1;
}
if (input < 1) {
zero = 0;
}
if (input = 1) {
zero = 1;
}
Here is what I've done in html. (We use codepen.io, which does not require body tags)
<form>
<input type="number" id="dec" min="0" max="255" step="1" />
</form>
<div id="seventh">0</div>
<div id="sixth">0</div>
<div id="fifth">0</div>
<div id="fourth">0</div>
<div id="third">0</div>
<div id="second">0</div>
<div id="first">0</div>
<div id="zeroth">0</div>
So getting down to the real question, once the user inputs a decimal number and I have the binary units saved to their corresponding variables, how do I route the value of these variables to the div elements I created in HTML?
Thanks so much for your help guys.