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' */
}

07-10-2012, 08:43 PM

dororo

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

07-11-2012, 01:16 AM

payukim

Quote:

Originally Posted by AhmedScript

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