Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Sep 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post Having problem with if else on JS

    I have problem with this code which is suposed to say 'Wrong Code' if the code is wrong but only says Gate oppening (supposed to recreate a gate program).
    Thanks
    Libeccio16

    [CODE]
    <!DOCTYPE html>
    <html>
    <body>

    <p>Click the button to enter the code</p>

    <button onclick="myFunction()">Code</button>

    <p id="demo"></p>
    <script>
    function myFunction()
    {
    var x="Code";
    var code=prompt("Enter Code")
    if (x==1610)
    {
    x=alert("Gate Openning");
    }
    else
    {
    alert("Wrong Code");
    }

    }
    </script>
    </body>
    </html>

    [CODE]

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,079
    Thanks
    23
    Thanked 592 Times in 591 Posts
    Code:
    <!DOCTYPE html>
    <html>
    <body>
    
    <p>Click the button to enter the code</p>
    <button onclick="myFunction()">Code</button>
    <p id="demo"></p>
    
    <script>
    function myFunction(){
    	var x="Code";                          // THIS IS NOT USED    
    	var code = prompt("Enter Code")
    	if (code == 1610)                    // ERROR HERE
    	{
    		alert("Gate Openning");    // X =  NOT NEEDED
    	}else{
    		alert("Wrong Code");
    	}
    }
    </script>
    </body>
    </html>
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •