
Page /> 1 | 2 | 3
The Function:
The first thing you can see "strPennies.substring(0, len - 2)", will return all characters inside "strPennies" except the last 2 (we start from 0, and end in "Len - 2", since we set Len to the value of the number of characters inside "strPennies", then Len - 2 will be the number of characters inside "strPennies" - 2). The part which says "strPennies.substring(Len - 2, Len)" will return the last 2 characters of string "strPennies" ("Len - 2", the offset, will start retrieving characters from 2 characters before the end of the string and will retrieve only 2 characters, thus the last 2 characters of "strPennies").
Now what the " + "." + " will do is create a string with all the characters of the "strPennies" except the last 2, a dot, and then the last 2 characters of "strPennies". Now the number is in the format we wanted it in, but one more problem, it is still stored as a string and we want it as a number. Now we call "parseFloat" on the returned value we had before which will turn the numbers inside the string into a number, then this number is returned. This can get confusing if you don't try it out yourself, so I suggest you try to write your own simple functions. I am including several sample functions you can "play" around with in order to fully understand how functions work.
function return_10 {
return 10;
}
// Will return the number 10.
Function return_10_s {
return "10";
}
// Will return a string with the number 10.
Function myproduct (myarg, myarg2) {
return myarg * myarg2;
}
// Will return the product of the two arguments passed to it (ex. product = myproduct(10,3))
The Output:
Now that you get the gist of functions, lets move on to the main function, generate_page.
This function is called when the user presses the "Submit" button (we made sure this work by adding "onClick="generate_page(this.form)"" into the submit button field tag in line 51), and as an argument the current form is passed. The current form is now "form" or the argument of "generate_page" as you can see in "(form)" at the beginning of the function.
The next two lines (20 and 21) define global variables "tax" and "delivery_p" (global variables can be accessed from anywhere in the code, unlike local variables which are declared with "var"). We set the "tax" to 0.08 and "delivery_p" to 2.99, which will later use to calculate the final price for the order on this form.
On line 22 we create "odate" as a local Date object, which we will later use to print out the information about when the product was ordered. Now we finally need to use the current form that was set as "form" in the argument of the "generate_page" function. We set the local variable "qty" to the vale of the "quantity" field (which you can see on line 50) on the current form (which is now "form"), and then this variable is then left alone until we need it later on in the code in order to calculate the final price.
On line 24, the local variable "product_v" is created as a new String object with the value of "form.product.value" (which will be the value of the currently selected value in the select field called "product" on the current form), and now we get to the semi-hard part. Since the "product" field contains text in the format such as "Brown Pants - Size 31 - $58.99" and we only need to get the price, we have to "look" (parse) through the string "product_v" in order to determine the price. We set the value of line "total_price" to the value of "product_v" starting at the character after the first "$" (dollar character) in "product_v" and ending at the end of the string (so if we had a string with "Hello $5656.34565" this would retrieve the "5656.34565").
Now we need to calculate the total price without tax, which we do by multiplying the quantity which is stored in "qty" and the price per product which is stored in "total_price", but we still need this in the right format so we call the round_total function with this value.
You can understand the next few lines if you were able to understand the one I just explained. So, I am going to skip over to line 30, the document object has a function writeln (there is also a write function but it does not append a "\n" or newline character to the end of the string), this is the part where we actually print the receipt. There is something interesting you might happen to notice on line 35, we use the odate object (Date object) again. We call the method toGMTString on the object which will return the current date in a readable form, which we also print out on the receipt.
That's it! You've lived through it, now I suggest you practice writing you own functions in order to remember what you've learned.
<< Previous Page