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,716
    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' */
     }
    signature under construction

  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 01: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

"

"

X vBulletin 4.2.2 Debug Information

  • Page Generation 0.17077 seconds
  • Memory Usage 2,906KB
  • Queries Executed 15 (?)
More Information
Template Usage (34):
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (1)bbcode_code
  • (2)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (7)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (7)postbit
  • (7)postbit_onlinestatus
  • (7)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (73):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_postinfo_query
  • fetch_postinfo
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates