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 4 of 4
  1. #1
    New Coder
    Join Date
    Oct 2010
    Location
    Toronto
    Posts
    95
    Thanks
    52
    Thanked 0 Times in 0 Posts

    Question I need help with variables

    I wrote a script: when the same button is clicked for the first time, it should give a message "Hello World", for the second time "hello Earth". The script is not working.
    I don't want anybody to re-write the script, but to look at how i define variables and tell me where I am wrong. I think, this is where the main error is.

    my current script just outputs the message "hello world".

    my assumption is that functions can alter the value of a global variable.


    Thank you very much.


    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
            "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title> title</title>
    <meta http-equiv="content-type" content="text/html;charset=UTF-8" />
    <script type="text/javascript">
    var count=0;
    
    function changePar() {
    if(count===0) 
    first();
    
    
    if(count===1) 
    second();
    }
    
    
    function first() {
    document.getElementById("changingParagraph").innerHTML="Hello World";
    var count=1;
    }
    
    function second() {
    document.getElementById("changingParagraph").innerHTML="Hello Earth";
    var count=0;
    }
    
    </script>
    
    </head>
    <body>
    <button onclick="changePar()">Click Here</button>
    <p id="changingParagraph"></p>
    </body>
    </html>
    Last edited by chickentulip; 01-16-2012 at 06:04 PM.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,731
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title> title</title>
    <meta http-equiv="content-type" content="text/html;charset=UTF-8" />
    <script type="text/javascript">
    
    var count=0;
    
    function changePar() {
    
    if(count==0) {
    first();
    return false;  // terminate
    }
    
    if(count==1) {
    second();
    return false;  // terminate
    }
    
    }
    
    
    
    function first() {
    document.getElementById("changingParagraph").innerHTML="Hello World";
    count=1;  // no var prefix
    }
    
    function second() {
    document.getElementById("changingParagraph").innerHTML="Hello Earth";
    count=0;  // no var prefix
    }
    
    </script>
    
    </head>
    <body>
    <button onclick="changePar()">Click Here</button>
    <p id="changingParagraph"></p>
    </body>
    </html>
    a) the var prefix within a function makes the variable local to that function, and hence not available to other functions. Your count variable is global scope, but the value never changes. In your code you have three different variables, all named "count". They do not conflict, as one is global and two are local. But obviously it is a bad idea to give the same name to two different variables.

    b) You need to add return false to the changePar() function, as indicated. Otherwise first() runs and immediately changes the value of count to 1, so causing second() to run.

    c) It is always a bad idea to leave out the braces surrounding an if or for statement, even if only one line of code follows.

    d) == is comparison - equal to. === is exactly equal to (value and type). It is not required here.


    BTW, when posting here please help us to help you by following the posting guidelines and wrapping your code in CODE tags. This means use the octothorpe or # button on the toolbar. You can (and should) edit your previous post.




    Quizmaster: In The United States a patent was granted on March 7th 1876 to Alexander Graham Bell for which invention?
    Contestant: The lightbulb
    Last edited by Philip M; 01-16-2012 at 05:53 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    chickentulip (01-16-2012)

  • #3
    Regular Coder
    Join Date
    Aug 2010
    Posts
    945
    Thanks
    19
    Thanked 205 Times in 203 Posts
    var count=0;// this count is global

    function changePar() {
    if(count===0)
    first();


    if(count===1)
    second();
    }


    function first() {
    document.getElementById("changingParagraph").innerHTML="Hello World";
    var count=1;//because you have used the var keyword this local
    count is not the same variable as the global count, they are two
    different variables. Leave off the var and they will be the same
    variable.
    }

  • #4
    New Coder
    Join Date
    Oct 2010
    Location
    Toronto
    Posts
    95
    Thanks
    52
    Thanked 0 Times in 0 Posts
    Thank you all


  •  

    Posting Permissions

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