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 totalPrice;
var salesPrice = window.prompt("Please Enter Purchase Price?", "");
var minShipping = salesPrice * 1.50/100;
var 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.

Does any of your variables that you are trying to add include the dollar sign ($)? The script is treating your variables are strings rather than numbers which is probably happening because at least one of them can't be converted and so they are being concatenated.
Also,

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

is nothing but a fancy IF statement. If (salesPrice <= 25) do first statement, else do second statement.

Are they really teaching the ternary operator x?y:z before if(){...}? Most programnmers will have learned if() well before the ternary.

If you have learned the Math object then this will do it :

var salesPrice = Number(prompt("Please Enter Purchase Price?", ""));
var shipping = Math.max(1.50, salesPrice * 100 / 10);
var totalPrice = salesPrice + shipping;
alert(totalPrice);

Explanation:

  • Number() ensures the string returned by prompt() is converted to a number. If the string entered is not a number then Number() will convert it to the the special javascript value NaN (Not-a-Number).
  • Math.max(x, y) takes the larger of the two numeric values x and y. By taking the larger of 1.50 and salesPrice * 100 / 10, a minimum of 1.50 is imposed on the calulated value. To many, this will be counter-intuitive.
  • Use of Math.max() avoids having to use either if(){...} or the ternary operator.
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.