【问题标题】:New to JS and having trouble!JS新手,遇到麻烦了!
【发布时间】:2011-04-24 00:43:37
【问题描述】:

我是 JavaScript 新手,我整天都在做这个任务,我非常沮丧。我无法弄清楚我做错了什么或我可能错过了哪些代码。非常感谢任何帮助!下面是我已经完成的作业和代码。谢谢!!

任务: 许多公司通常会收取购买的运费和手续费。创建一个 允许用户在文本框中输入购买价格并包含 计算运输和处理的 JavaScript 函数。向脚本添加功能 对于任何低于 大于或等于 25.00 美元。对于任何超过 25.00 美元的订单,在总购买价格上加 10% 用于运输和处理,但不包括 1.50 美元的最低运输和处理 收费。计算百分比的公式是价格 * 百分比 / 100。例如, 计算 50.00 美元购买价格的 10% 的公式是 50 * 10 / 100,结果是 运费和手续费 5.00 美元。在您确定订单的总成本后 (购买加上运输和处理),将其显示在警报对话框中。保存文档 作为 CalcShipping.html。

代码编写:

<!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>

【问题讨论】:

    标签: javascript function syntax


    【解决方案1】:

    这是你写的:

    getShipping();
    {
        if (price <= 25.00)
        shipping = 1.50
        else (price > 25.00)
        shipping = (price * (10/100));
    }
    

    这是调用一个当前未定义的函数,然后进入一个块。要定义一个函数,你必须使用function关键字:

    function getShipping()
    {
        if (price <= 25.00)
        shipping = 1.50
        else (price > 25.00)
        shipping = (price * (10/100));
    }
    

    您还写了else (price &gt; 25.00)。您需要在else 之后添加if(使其成为else if (price &gt; 25.00))或删除(price &gt; 25.00)

    【讨论】:

      【解决方案2】:

      这段代码有错误:

      else (price > 25)
      

      你在 else 之后缺少一个 if:

      else if (price > 25)
      

      但是,由于第二个条件与第一个条件完全相反,因此您不需要它。您可以使用:

      else
      

      【讨论】:

        猜你喜欢
        • 2020-11-02
        • 2020-06-15
        • 1970-01-01
        • 2016-06-29
        • 2021-06-06
        • 1970-01-01
        • 1970-01-01
        • 2016-08-12
        • 2017-08-25
        相关资源
        最近更新 更多