-5

Possible Duplicate:
In JavaScript doing a simple shipping and handling calculation

Many companies normally charge a shipping and handling charge for purchases. Create a Web page that allows a user to enter a purchase price into a text box and includes a JavaScript function that calculates shipping and handling. Add functionality to the script that adds a minimum shipping and handling charge 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 charge. The formula for calculating a percentage is price * percent / 100. For example, the formula for calculating 10% of a $50.00 purchase price is 50 * 10 / 100, which results in a shipping and handling charge of $5.00. After you determine the total cost of the order (purchase plus shipping and handling), display it in an alert dialog box.

<!DOCTYPE>
<html><head>
<title>Project Two</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />

<script type="text/javascript">
/*<CDATA[[*/
var salesPrice = window.prompt("Please Enter Purchase Price?", "");
minShipping = salesPrice * 1.50/100;
maxShipping = salesPrice * 10/100;
(salesPrice <= 25)? totalPrice = salesPrice + minShipping
    : totalPrice = salesPrice + maxShipping;
alert(totalPrice);
/*]]>*/
</script>
</head>

I please need to have all my script checked and note that, we're still in chapter 2 thus (if) statements are not allowed to be used yet. Thanks.

Community
  • 1
  • 1
user1525154
  • 3
  • 1
  • 3
  • Geez, at least rephrase the question to not look like you're copy-pasting from a homework assignment. – Lior Cohen Jul 14 '12 at 06:36
  • 1
    And tell your teacher his/her is wrong :P – Lior Cohen Jul 14 '12 at 06:37
  • 6
    You know what's funny? https://encrypted.google.com/search?q=Many+companies+normally+charge+a+shipping+and+handling+charge+for+purchases – Adi Jul 14 '12 at 06:39
  • I typed "javascript convert string to number" in google, and this aw the first hit: http://www.javascripter.net/faq/convert2.htm Remember that when you enter the salesPrice, you are getting a *string* from the user. – Dilum Ranatunga Jul 14 '12 at 06:40
  • You know ternary operators are basically concatenated `if`/`else` statements right? You learn ternaries before `if`/`else`? – Fabrício Matté Jul 14 '12 at 11:13

1 Answers1

0

First of all, your ternary operator seems to be quite repetetive. There is a way to make it shorter:

totalPrice = salesPrice + ( salesPrice <= 25 ? minShipping : maxShipping );

Then, it is a good habit to declare your variables before you use them using var keyword. In this particular case the advice is useless, since the script will be executed in the global scope, so all your variables will end up being global.

Your version of script concatenates the result since the return type of window.prompt is String. And when the one tries to add a String to a Number, the latter just gets converted into a String and the concatenation occurs.

You can explicitly convert String to Number by using built-in parseInt (or parseFloat) functions or by adding a plus sign.

<script type="text/javascript">
/*<CDATA[[*/
var salesPrice, minShipping, maxShipping, totalPrice;

salesPrice  = +window.prompt( 'Please Enter Purchase Price?', '' ),
minShipping = salesPrice * 1.5 / 100;
maxShipping = salesPrice * 10  / 100;

totalPrice  = salesPrice + ( salesPrice <= 25 ? minShipping : maxShipping );

alert( totalPrice );
/*]]>*/
</script>
Lapple
  • 3,385
  • 20
  • 20