Javascript: 制作收银机主题网站并计算给定项目的总数

Javascript: Make cash register themed website and calculate the total from the given items

这是我第一次发帖,所以如果我违反了任何规则,请原谅我。我知道有一些关于收银机的答案,但我的任务是制作收银机主题网站,我使用提示进行订购,并在网页上给出总数。我已经得到这段代码来计算小计,但我无法弄清楚为什么没有计算总数,还有一件事如何在使用 console.log 时获得 document.write 来给出总数。请帮忙。谢谢。

<!doctype html>
<html>
<head>
meta charset="utf-8">
<title>Please Make this Work!</title>
 </head>
  <body>
   <script>
    var items = ["Bananas", "Kiwis", "Apples", "Pears", "Orange"];
    var numItems = new Array();
     for (i=0; i<items.length; i++) {
     numItems.push(prompt("How Many " +items[i] +" do you want?"));
     console.log(numItems);
    }
   var $items= [3, 4, 3.5, 1, 0.75];

   for (i=0; i<items.length; i++) {
    var itemTotal=new Array();
    itemTotal= ($items[i]*numItems[i]);
        console.log(itemTotal);
    for (it=0; i<itemTotal.length; it++) {
    var x = itemTotal[it];
    var total = x + itemTotal[it];
    console.log(total);
        }
  }
  document.write(total);
 </script>

</body>
</html>

您的脚本中存在一些逻辑缺陷。请注意 for-loop 中应包含的内容以及在某个循环后需要执行的内容。虽然需要勾选variable-types,例如var a = 12 + "2"会变成a = "122"。使用 parseInt (var) 解析输入到提示中的数字。要增加变量,请执行 var a = 0; var a += 2。之后 var a 将是 2.

我修复了你的代码:

<html>
    <head>
    <meta charset="utf-8">
    <title>Please Make this Work!</title>
    </head>
    <body>
        <script>
            var items = ["Bananas", "Kiwis", "Apples", "Pears", "Orange"];
            var numItems = new Array();
            for (i=0; i<items.length; i++) {
                numItems.push(prompt("How Many " +items[i] +" do you want?"));
                console.log(numItems);
            }
            var $items= [3, 4, 3.5, 1, 0.75];

            var itemTotal=new Array();
            for (i=0; i<items.length; i++) {
                itemTotal.push ($items[i]*parseInt(numItems[i]));
                console.log(itemTotal);
            }

            var total = 0;
            for (j=0; j<itemTotal.length; j++) {
                total += itemTotal[j];
                console.log(total);
            }
            document.write(total);
        </script>

    </body>
</html>