Isssue With my js, css, html program

Started on the old forum in November 2016 · view the original in the archive · old links redirect here

4 replies · 151 views · started 10 years ago · 1 min read

3 participants
Gtmanic Newbie · 1 posts #1

hi i am trying to avoide using the toString function and instead using arrays for denary to binary, but my program doesn't work. i know it has to do with syntax errors but could someone show me them? thank you!

<!DOCTYPE html> <html> <body> <title> Denary to Binary </title> <header class="Header"> <h1> Denary to Binary Converter </h1> </header>

please insert a denary number below 255 (8bit)

<form id = "myform"> <input id="denary" type="text" placeholder="input number here"> <button type="submit" class="btn">enter</button> </form> <form id ="myText"> <input id="binary" class="btn" type="text" placeholder="answer"> </form> <style> html, body { font-family: 'Arial Black", Gadget, sans-serif'; h1 { font-size: 50px; border-bottom: 2px solid green; padding-left: 20px } .btn { paddding: 13px 15px; border: none; background-color: #D3D3D3; } </style> <script> function DeToBi() { var bi = [] var De = document.getElementById("denary").value; if (De > 255) { document.getElementById("myText").value = "your number is bigger than 255"; } else if (Dee - 128 >= 0) { De = De - 128; bi.push("1"); } else { De = De - 0; bi.push("0") } if (De - 64 >= 0) { De = De - 64 ; bi.push("1"); } else { De = De - 60; bi.push("0"); } if (De - 32 >= 0) { De = De - 32; bi.push("1"); } else { De = De - 0; bi.push("0"); } if (De - 16 >= 0) { De = De - 16; bi.push("1"); } else { De = De - 0; bi.push("0"); } if (De - 8 >= 0) { De = De - 8; bi.push("1"); } else { De = De - 0; bi.push("0"); } if (De - 4 >= 0) { De = De - 4; bi.push("1"); } else { De = De - 0; bi.push("0"); } if (De - 2 >= 0) { De = De - 2; bi.push("1"); } else { De = De - 0; bi.push("0"); } if (De - 1 >=0) { De = De - 1; bi.push("1"); } else { De = De - 0; bi.push("0"); } document.getElementById("myText").innerHTML = bi; </script> </body> </html>
something else Fellow · 2,296 posts #2

Hi there - You have a lot of problems in your code. Here is a fixed version:

<!DOCTYPE html>
<html>
    <body>
        <title> Denary to Binary </title>
        
        <header class="Header">
            <div class="border">
            
                <div class="space">
                    <h1> Denary to Binary Converter </h1>
                </div>
                
            </div>
        </header>
        
        <div class ="divider">
            <p class ="body"> please insert a denary number below 255 (8bit) </p>
        </div>
        
        <form id = "myform">
            <input id="denary" type="text" placeholder="input number here">
            <button id="submit" type="submit" class="btn">enter</button> <!--added id="submit" -->
        </form>
        
        <form id ="myText">
            <input id="binary" class="btn" type="text" placeholder="answer">
        </form>
        
        <style> 
            html,
            body 
            {
                font-family: 'Arial Black", Gadget, sans-serif';
            }
            h1 
            {
                font-size: 50px;
                border-bottom: 2px solid green;
                padding-left: 20px
            }
            .btn 
            {
                paddding: 13px 15px;
                border: none;
                background-color: #D3D3D3;
            }
        </style>
                
        <script> 
            //Added event listener
            var submit = document.getElementById("submit");
            submit.addEventListener("click", function(event) 
            {
                event.preventDefault(); //prevent form from reloading page
                DeToBi();
            }, false);
            
            function DeToBi() 
            {
                var bi = []; //closed line with ";"
                var De = document.getElementById("denary").value;
                if (De > 255) 
                {
                    document.getElementById("binary").value = "your number is bigger than 255"; //Chnaged myText to binary
                } 
                else if (De - 128 >= 0) //Changed Dee to De
                {
                    De = De - 128;
                    bi.push("1");
                }
                else 
                {
                    De = De - 0;
                    bi.push("0")
                }
                
                if (De - 64 >= 0) 
                {
                    De = De - 64;
                    bi.push("1");
                } 
                else 
                {
                    De = De - 60;
                    bi.push("0");
                }
                
                if (De - 32 >= 0) 
                {
                    De = De - 32;
                    bi.push("1");
                } 
                else 
                {
                    De = De - 0;
                    bi.push("0");
                }
                
                if (De - 16 >= 0) 
                {
                    De = De - 16;
                    bi.push("1");
                } 
                else 
                {
                    De = De - 0;
                    bi.push("0");
                }
                
                if (De - 8 >= 0) 
                {
                    De = De - 8;
                    bi.push("1");
                } 
                else 
                {
                    De = De - 0;
                    bi.push("0");
                }
                
                if (De - 4 >= 0) 
                {
                    De = De - 4;
                    bi.push("1");
                } 
                else 
                {
                    De = De - 0;
                    bi.push("0");
                }
                if (De - 2 >= 0) 
                {
                    De = De - 2;
                    bi.push("1");
                } 
                else 
                {
                    De = De - 0;
                    bi.push("0");
                }
                
                if (De - 1 >= 0) 
                {
                    De = De - 1;
                    bi.push("1");
                } 
                else 
                {
                    De = De - 0;
                    bi.push("0");
                }
                
                //Delete these 2 lines out if you don't want to use toString
                bi = bi.toString(); //Added toString - needed so you can use replace()
                bi = bi.replace(/,/g, ""); //replace "," with ""
                
                
                document.getElementById("binary").value = bi; //changed myText to Binary and innerHtml to value
            } //closed function here
        </script>
    </body>
</html>
something else Fellow · 2,296 posts #3

There is also more problems in your code in which all numbers under 61 all convert to 00000000.

Gtmanic Newbie · 1 posts #4

What is an event listener?

Gtmanic Newbie · 1 posts #5

Oh and i found the problem for under 61, there was a 60 instead of a 0.

something else Fellow · 2,296 posts #6

Gtmanic wrote: What is an event listener?

Its to detect when your submit button has been pushed.

The other way you could do it without an event listener is by adding the function into the html eg:

<button class="btn" onclick="DeToBi();">enter</button>

It is better to use an event listener.

andre3 Developer · 192 posts #7

something else wrote: Hi there - You have a lot of problems in your code. Here is a fixed version:

<!DOCTYPE html>
<html>
<body>
<title> Denary to Binary </title>

<header class="Header">
<div class="border">

<div class="space">
<h1> Denary to Binary Converter </h1>
</div>

</div>
</header>

<div class ="divider">
<p class ="body"> please insert a denary number below 255 (8bit) </p>
</div>

<form id = "myform">
<input id="denary" type="text" placeholder="input number here">
<button id="submit" type="submit" class="btn">enter</button> <!--added id="submit" -->
</form>

<form id ="myText">
<input id="binary" class="btn" type="text" placeholder="answer">
</form>

<style>
html,
body
{
font-family: 'Arial Black", Gadget, sans-serif';
}
h1
{
font-size: 50px;
border-bottom: 2px solid green;
padding-left: 20px
}
.btn
{
paddding: 13px 15px;
border: none;
background-color: #D3D3D3;
}
</style>

<script>
//Added event listener
var submit = document.getElementById("submit");
submit.addEventListener("click", function(event)
{
event.preventDefault(); //prevent form from reloading page
DeToBi();
}, false);

function DeToBi()
{
var bi = []; //closed line with ";"
var De = document.getElementById("denary").value;
if (De > 255)
{
document.getElementById("binary").value = "your number is bigger than 255"; //Chnaged myText to binary
}
else if (De - 128 >= 0) //Changed Dee to De
{
De = De - 128;
bi.push("1");
}
else
{
De = De - 0;
bi.push("0")
}

if (De - 64 >= 0)
{
De = De - 64;
bi.push("1");
}
else
{
De = De - 60;
bi.push("0");
}

if (De - 32 >= 0)
{
De = De - 32;
bi.push("1");
}
else
{
De = De - 0;
bi.push("0");
}

if (De - 16 >= 0)
{
De = De - 16;
bi.push("1");
}
else
{
De = De - 0;
bi.push("0");
}

if (De - 8 >= 0)
{
De = De - 8;
bi.push("1");
}
else
{
De = De - 0;
bi.push("0");
}

if (De - 4 >= 0)
{
De = De - 4;
bi.push("1");
}
else
{
De = De - 0;
bi.push("0");
}
if (De - 2 >= 0)
{
De = De - 2;
bi.push("1");
}
else
{
De = De - 0;
bi.push("0");
}

if (De - 1 >= 0)
{
De = De - 1;
bi.push("1");
}
else
{
De = De - 0;
bi.push("0");
}

//Delete these 2 lines out if you don't want to use toString
bi = bi.toString(); //Added toString - needed so you can use replace()
bi = bi.replace(/,/g, ""); //replace "," with ""


document.getElementById("binary").value = bi; //changed myText to Binary and innerHtml to value
} //closed function here
</script>
</body>
</html>

Good job man, you are a real trooper.

Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close