Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-06-2012, 07:20 AM   PM User | #1
lostwalker
New to the CF scene

 
Join Date: Sep 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
lostwalker is an unknown quantity at this point
Calling functions and creating specific functions

Hi. I am new to this forum. I am taking a JavaScript course and am a bit lost. I always get confused what exactly to write in a function to get it to do what I want. My homework assignment asks me to do the following:

Create a web page that allows a user to enter a purchase price into a text box; include a JavaScript function that calculates shipping and handling. Add functionality to the script that adds a minimum shipping and handling fee of $1.50 for any purchase that is less than or equal to $25.00. For any orders over $25.00, add 10% to the total purchase price for shipping and handling, but do not include the $1.50 minimum shipping and handling fee. The formula for calculating a percentage is price * percent / 100. For example, the formula for calculating 10% of a $50.00 purchasse price is 50 * 10 / 100, which results in a shipping and handling fee or $5.00. After you determine the toal cost of the order (purchase plus shipping and handling), display it in an alert dialog box. Save the document as CalcShipping.html.

And this is what I have:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Project 3-1 Pg. 187</title>

<script type="text/javascript">

/* <![CDATA[ */


function ship_hand (price) {
var price;
var min_fee = 1.50;
var max_fee = (price * 10 / 100);

if ( price <= 25.00)
window.alert ( 'price += min_fee');
else if ( price > 25.00)
window.alert ( 'price += max_fee');

}

/* ]]> */

</script>


</head>

<body>


<form name="calcShipping" action="" method="get">

<p>
Purchase Price: <input type="text" name="price" value="0" onclick="ship_hand()" />
<input type="button" value="View Total" onclick="ship_hand()" />


</p>

</form>



</body>
</html>

* I am mostly confused on how to pass the purchase price entered by the user to the function so that it can calculate the shipping and handling charge. Please help.
lostwalker is offline   Reply With Quote
Old 09-06-2012, 08:50 AM   PM User | #2
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,260
Thanks: 10
Thanked 532 Times in 526 Posts
devnull69 will become famous soon enough
There are a few things that are just wrong as you will soon see.

First of all, there are many ways to retrieve the value from a form field using Javascript. As the button you clicked is still part of the form, you could refer to the price field value by this.form.price.value (this.form is a reference to the form. Every named field is a property of this form element and .value is the value property of this element)
Code:
<input type="button" value="View Total" onclick="ship_hand(this.form.price.value)" />
Your function ship_hand() doesn't look that bad, but

- You must not define a new var price inside your function because you are already passing a defined "price" variable as a parameter. A new definition would overwrite the passed parameter
- The value of a form field is always of type String. So the first thing you need to do if you want to use it for calculation or for comparison with numeric values is to convert it to a number
- You are using string concatenation wrong inside your alert() methods
Code:
function ship_hand (price) {
   price = parseFloat(price);
   var min_fee = 1.50;
   var max_fee = (price * 10 / 100);

   if ( price <= 25.00)
      window.alert ( price + min_fee);
   else
      window.alert ( price + max_fee);
}
devnull69 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:55 AM.


Advertisement
Log in to turn off these ads.