www.webdeveloper.com
Results 1 to 7 of 7

Thread: First Post, please help me with this script

  1. #1
    Join Date
    Jul 2012
    Posts
    6

    Exclamation First Post, please help me with this script

    <!DOCTYPE>
    <html><head>
    <title>Chapter Two</title>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
    <script type="text/javascript">
    /*<CDATA[[*/

    var guestsCost=0;
    var limousinesCost=0;
    var liveMusicCost=0;
    var flowersCost=0;
    var totalEstimate=0;

    function calcGuests(){
    totalEstimate -= guestsCost;
    guestsCost = document.details.numGuests.value * 65;
    totalEstimate += guestsCost;
    document.estimate.cost.value = "$" + totalEstimate;
    }
    /*]]>*/
    </script>
    </head>

    <body>
    <form action="" name="details">
    <table border="1">
    <tr>
    <td>Guests<br />
    ($65 each)</td>
    <td>
    <input type="text" name="numGuests" size="5" onchange="calcGuests();" /></td>
    </tr>
    </table>

    </form>
    <form action="" name="estimate">
    <p>
    <input type="text" name="cost" size="5"
    style="border-style: none; border-color: inherit border-width: medium; background-color: yellow; text="0" /></p>
    </form>


    </body>
    </html>

    I didn't understand what the second statement (totalEstimate -= guestsCost in the "calcGuests" function is for?
    Please someone explain to me as I'm a beginner.

    Thanks in Advance!

  2. #2
    Join Date
    Jul 2012
    Posts
    22
    (totalEstimate -= guestsCost) = (totalEstimate=totalEstimate-guestsCost)

    or it means subtract "total estimate" by "guest cost" and save the result in "total estimate".

  3. #3
    Join Date
    Jul 2012
    Posts
    6
    Quote Originally Posted by payukim View Post
    (totalEstimate -= guestsCost) = (totalEstimate=totalEstimate-guestsCost)

    or it means subtract "total estimate" by "guest cost" and save the result in "total estimate".
    Thank u for u quick reply.

    I changed the value of the variable totalEstimate and assigned it a value of 5 as : var totlaEstimate = 5;

    Now, if, lets say, I type 1 in the textbox.. I got a result of of 70?

    Please, if possible, explain to me the whole process/calculations that are taking place and how?

  4. #4
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,671
    Code:
    var guestsCost=0;
     var limousinesCost=0;/* this variable does nothing */
     var liveMusicCost=0;/* this variable does nothing */
     var flowersCost=0;/* this variable does nothing */
     var totalEstimate=0;
    
     function calcGuests(){
     totalEstimate -= guestsCost; /* for the very first calculation this line has no sense because it is equal to 'totalEstimate = 0 - 0;' */
     guestsCost = document.details.numGuests.value * 65;/* this means 'guestsCost = here_goes_what_you_type_in_the_first_form_input * 65' */
     totalEstimate += guestsCost;/* this is equal to 'totalEstimate = totalEstimate + guestsCost' */
     document.estimate.cost.value = "$" + totalEstimate;/* this makes a string from the dollar sign and the calculation result and puts it into the second form's INPUT element named 'COST' */
     }

  5. #5
    Join Date
    Jul 2012
    Posts
    5
    totalEstimate -= guestsCost;
    first time,it's mean totalEstimate = totalEstimate-guestsCost;// totalEstimate=0-0;
    result:
    totalEstimate=0+first time input numGuest*65;
    second time,your change the input numGuests,the js will run again,but the varables is global,
    totalEstimate!=0
    totalEstimate=first time input numGuest*65+second time input numGuest*65;//wrong answer!
    you must make totalEstimate=0 as the first time

  6. #6
    Join Date
    Jul 2012
    Posts
    22
    Quote Originally Posted by AhmedScript View Post
    Thank u for u quick reply.

    I changed the value of the variable totalEstimate and assigned it a value of 5 as : var totlaEstimate = 5;

    Now, if, lets say, I type 1 in the textbox.. I got a result of of 70?

    Please, if possible, explain to me the whole process/calculations that are taking place and how?
    Yes, you will get 70 as the result. Because:

    Let's say, you declare:

    guestsCost=0
    totalEstimate = 5


    Then you type 1 as the value in the text box name = numGuests

    After that it will call the function calcGuests() as you have onchange="calcGuests()" in the text box

    In the function, it does:

    totalEstimate -= guestsCost means totalEstimate = 5-0; totalEstimate now = 5

    guestsCost = document.details.numGuests.value * 65 means guestsCost = 1*65; guestsCost now = 65

    totalEstimate += guestsCost means totalEstimate = 5+65; totalEstimate now = 70

    document.estimate.cost.value = "$" + totalEstimate means it puts value $70 to the text box named "cost"

    That's all. Hope this can help.
    Last edited by payukim; 07-11-2012 at 02:18 AM.

  7. #7
    Join Date
    Jul 2012
    Posts
    6

    resolved

    Thanks to both of u, it really helped me a lot.

    ur clear explanation made me to grasp it easily, payukim. Thanks once again!

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