New to JS and having trouble!
I am new to JavaScript and I have been working on this assignment all day and I am extremly frustrated. I cannot figure out what I have done wrong or what code I may have missed. Any help is greatly appreciated it!! Below is the the assignment and the code I have done already. Thank you!!
Assignment: 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 co开发者_JS百科st of the order (purchase plus shipping and handling), display it in an alert dialog box. Save the document as CalcShipping.html.
Code written:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//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" />
<link rel="stylesheet" href="js_styles.css" type="text/css" />
<title>Calculating Shipping & Handling</title>
<script type="text/javascript">
/* ![CDATA[ */
getShipping();
{
if (price <= 25.00)
shipping = 1.50
else (price > 25.00)
shipping = (price * (10/100));
}
/* ]]> */
</script>
</head>
<body>
<script type="text/javascript">
/* ![CDATA[ */
document.write("<h1>Purchase Price with Shipping</h1>");
/* ]]> */
</script>
<script type="text/javascript">
/* <![CDATA[ */
var price=prompt("What is your purchase price?");
getShipping();
document.write ("<p>The purchase price entered is $" + (price) + "</p>");
document.write ("<p>Shipping is $" + (shipping) + "</p>");
var total = price + shipping;
document.write("Your total price with shipping is $ " + (total) + "");
/* ]]> */
</script>
</body>
</html>
You wrote this:
getShipping();
{
if (price <= 25.00)
shipping = 1.50
else (price > 25.00)
shipping = (price * (10/100));
}
That's calling a currently undefined function and then enters a block. To define a function, you have to use the function
keyword:
function getShipping()
{
if (price <= 25.00)
shipping = 1.50
else (price > 25.00)
shipping = (price * (10/100));
}
You also wrote else (price > 25.00)
. You either need to add an if
after the else
(making it else if (price > 25.00)
) or drop the (price > 25.00)
.
You have an error in this code:
else (price > 25)
You are missing an if after the else:
else if (price > 25)
However, as the second condition is the exact inverse of the first, you don't need it. You can just use:
else
精彩评论