www.webdeveloper.com
Results 1 to 6 of 6

Thread: a very elementary JavaScript question

Hybrid View

  1. #1
    Join Date
    Apr 2013
    Posts
    3

    a very elementary JavaScript question

    <!DOCTYPE html>
    <html>
    <body>

    <p>Given that y=5, calculate x=y+2, and display the result.</p>
    <button onclick="myFunction()">Try it</button>

    <p id="demo"></p>

    <script>
    function myFunction()
    {
    var y=5;
    var x=y+2;
    var demoP=document.getElementById("demo")
    demoP.innerHTML="x=" + x;
    }
    </script>

    </body>
    </html>


    Can anybody please explain me why on the "demoP.innerHTML="x=" + x;", the function has to be written in "="x=" + x;"? I got this example from w3school, but nobody explains anything about it.
    Please help!!

  2. #2
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,373
    The "x=" is a string to be displayed in the demo.
    The x variable is the result of the calculation.

    Take out either temporarily and view the results.
    In one case you will see "x=" only and in the other you will see "7" only.

    The nice thing about the "Try it" page is that you can make quick changes and see the results immediately.

  3. #3
    Join Date
    Apr 2013
    Posts
    3
    thx JMRKER

    I have tried taking out either the "x=" or "+x",
    it's x= and 7 respectively.
    Is there any importance for writing "x=" in the function? Can I omit it?

    There is another example:
    <!DOCTYPE html>
    <html>
    <body>

    <p>Click the button to create a variable, and display the result.</p>

    <button onclick="myFunction()">Try it</button>

    <p id="demo"></p>

    <script>
    function myFunction()
    {
    var carname="Volvo";
    document.getElementById("demo").innerHTML=carname;
    }
    </script>

    </body>
    </html>


    As I have seen, the function(? sorry if I don't name the parts correctly) here doesn't have anything in quotation after innnerHTML.
    Therefore, is the "x="necessary to be put in the function? If so, what's the importance of that? Does it affect anything? I have tried, the factor affects the calculation is y or 2 only. If I change these 2, the output will be affected. To my understanding, x itself is enough for the output. Why bother writing "x="?

    Sorry for my clumsy reasoning, I haven't touched science for more than 10 years. I hope I hadn't annoyed you.

  4. #4
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,373
    The "x=" portion is just a string. Nothing more, nothing less.
    It is in the example to show the user what the new value of x (the variable) is (sum of 5+2).

    The "x=" has NO EFFECT on the actual calculation of x = 5+2;
    Change the string to a different display and see, for example, that you
    could substitute as "The old value of x = 5 has changed with the addition of +2 to a new value of x="+x;

    BTW: You should enclose your scripts between [ code] and [ /code] tags (without the spaces)
    to make it easier to see your program and preserve your formatting.

  5. #5
    Join Date
    Apr 2013
    Posts
    3
    can i ask you another question?


    <!DOCTYPE html>
    <html>
    <body>

    <p>Given that x=10 and y=5, calculate x%=y, and display the result.</p>
    <button onclick="myFunction()">Try it</button>

    <p id="demo"></p>

    <script>
    function myFunction()
    {
    var x=10;
    var y=5;
    x%=y;
    var demoP=document.getElementById("demo")
    demoP.innerHTML="x=" + x;
    }
    </script>

    </body>
    </html>


    Given that x=10 and y=5, calculate x%=y, and display the result.

    x=0

    Why x=0?

    when we use the operator %=, the calculation is x=x%y, given x=10, y=5
    the calculation should be x=5/100*10,
    =0.05*10
    =0.5

    why it's 0?

  6. #6
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,373

    Lightbulb

    The % character used here is a modulo operation, NOT the percentage as you seem to be expecting.

    Modulo operations are like division, but only the integer remainder is returned as a result.

    Therefore:
    10 % 5 is 0
    11 % 5 is 1
    12 % 5 is 2
    13 % 5 is 3
    14 % 5 is 5
    and
    15 % 5 is 0

    I would suggest you read about the Math. operations
    like +, -, *, /, %, Math.random(), Math.power(), etc.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles