Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/415.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript switch语句中的返回值_Javascript_Switch Statement_Return - Fatal编程技术网

Javascript switch语句中的返回值

Javascript switch语句中的返回值,javascript,switch-statement,return,Javascript,Switch Statement,Return,背景:我正在为学校作业创建一个模拟购物车的程序。我添加了一个switch语句来显示数据和计算项目小计。到目前为止,这是可行的,我可以计算小计,当选择两个相同的项目进行采购时,小计反映了批量项目的总数。我的问题在于将小计加在一起得到最终的总数 我应该把所有内容都分成if语句还是可以在switch语句的末尾返回一个完整的总数 非常感谢您的帮助 <!Doctype html> <html lang="en"> <head> <t

背景:我正在为学校作业创建一个模拟购物车的程序。我添加了一个switch语句来显示数据和计算项目小计。到目前为止,这是可行的,我可以计算小计,当选择两个相同的项目进行采购时,小计反映了批量项目的总数。我的问题在于将小计加在一起得到最终的总数

我应该把所有内容都分成if语句还是可以在switch语句的末尾返回一个完整的总数

非常感谢您的帮助

<!Doctype html>

<html lang="en">

<head>
    <title>""</title>
    <script>
        function CheckOut() {

            // variable declaration 

            let product = ["Winter Hat", "Warm Coat", "Gloves"];
            let prices = [27.50, 160.23, 23.99];

            let product_code = parseInt(document.forms.myform.product.value);
            let product_quanity = parseInt(document.forms.myform.quanity.value);

            let total = 0;

            /* generates a list of shopping items. Users will enter a product code (1-3) and the quanity of the product they wish to buy. 
             The sub total for these products are then added together to output an final total. */

            switch (product_code) {
                case 1:
                    document.getElementById("results").innerHTML += ("Winter Hat" + "[" + product_quanity + "]");
                    total += product_quanity * prices[0];
                    document.getElementById("results").innerHTML += "$" + total.toFixed(2) + "<br>";
                    break;

                case 2:

                    document.getElementById("results").innerHTML += ("Warm Coat" + "[" + product_quanity + "]");
                    total += product_quanity * prices[1];
                    document.getElementById("results").innerHTML += "$" + total.toFixed(2) + "<br>";
                    break;

                case 3:
                    document.getElementById("results").innerHTML += ("Gloves" + " [" + product_quanity + "]");
                    total += product_quanity * prices[2];
                    document.getElementById("results").innerHTML += "$" + total.toFixed(2) + "<br>";

                    break;

                default:
                    alert("Please input a number between 1 - 3.");

            }

         
        }
    </script>
</head>

<body>

    This is the point of sale program for a store selling winter cloths.
    <ul> 
        <li>Product Code 1 : A Winter Hat costs $27.50</li>
        <li>Product Code 2 : A Warm Coat costs $160.23</li>
        <li>Product Code 3 : A pair of gloves costs $32.99</li> 
    </ul>

    <form name=myform action="javascript:CheckOut()">

        Product Code: <input type="text" name="product" id="product"><br>
        Quanity: <input type="text" name="quanity" id="quanity"><br>

        <input type="submit" value="Submit">
    </form>

    <div id="results"></div>

</body>
<html>
    

""
函数签出(){
//变量声明
let product=[“冬季帽子”、“保暖外套”、“手套”];
让价格=[27.50160.2323.99];
让product_code=parseInt(document.forms.myform.product.value);
让product_quanity=parseInt(document.forms.myform.quanity.value);
设total=0;
/*生成购物项目列表。用户将输入产品代码(1-3)和他们希望购买的产品数量。
然后将这些产品的小计加在一起,以输出最终总数*/
开关(产品代码){
案例1:
document.getElementById(“结果”).innerHTML++=(“冬季帽子”+“[“+产品质量+”]);
合计+=产品数量*价格[0];
document.getElementById(“结果”).innerHTML+=“$”+total.toFixed(2)+“
”; 打破 案例2: document.getElementById(“结果”).innerHTML++=(“保暖外套”+“[“+产品质量+”]); 合计+=产品数量*价格[1]; document.getElementById(“结果”).innerHTML+=“$”+total.toFixed(2)+“
”; 打破 案例3: document.getElementById(“结果”).innerHTML++=(“手套”+“[“+产品质量+”]); 合计+=产品数量*价格[2]; document.getElementById(“结果”).innerHTML+=“$”+total.toFixed(2)+“
”; 打破 违约: 警报(“请输入1-3之间的数字”); } } 这是一家销售冬衣的商店的销售点计划。
  • 产品代码1:一顶冬季帽子售价27.50美元
  • 产品代码2:一件保暖外套的价格为160.23美元
  • 产品代码3:一副手套售价32.99美元
产品代码:
数量:

我可以在switch语句末尾返回完整的总计吗?看起来您的checkout函数只是给出每个项目的小计。如果您想让它计算总金额,您需要跟踪他们“添加”到购物车中的每个项目的产品代码和数量。我之所以说“添加”,是因为现在你没有将这些信息存储在任何地方,所以目前你的系统不能有效地“添加到购物车”。