使用Javascript

时间:2018-02-23 01:49:57

标签: javascript arrays loops javascript-objects

你好,所以我正在尝试为我的Java Script类处理这段代码,但我仍然坚持如何继续。我们给出的问题说明如下:

创建一个简单的自助结账系统。提示三个项目的价格和数量。计算项目的小计。然后使用5%的税率计算税额。打印出包含数量和总数的订单项,然后打印小计,税额和总额。

到目前为止,这是我的代码:

// Make a function for a simple self-checkout system. 
// prompt the user for quantity of the items
// Prompt the user for the prices of the items

function self_Checkout () {


  var prices = [x, y, z,];
  var x = prompt('Enter value');
  var quantity_x = prompt('Enter value for quantity of item 1');
  return x * quantity_x;}
{ if 
  var y = prompt('Enter value');
  var quantity_y = prompt('Enter value for quantity of item 2');
  return y * quantity_y;
{ if
  var z = prompt('Enter value');
  var quantity_z = prompt('Enter value for quantity of item 3');
  return z * quantity_y;


  // Multiply entire total by a tax rate of 5%
  // Return value of total of all items + tax to user 
  // Use console.log or document.write?

}

现在,赋值还提到我们应该如何在这个问题中使用循环对象和数组。我试图在代码中添加一个数组。一些关于如何继续我的代码的帮助将非常感激,希望我解释得很好,以获得一些帮助。

1 个答案:

答案 0 :(得分:1)

以下是全部动态的工作代码,不限于3个项目

<!DOCTYPE html>
<html>
<head>
<script>
function getit(){
    var result = document.getElementById('demo');
        var allitems = 0;
            var itemCount = prompt("how many items do you need?");
                var items = {};
for (i = 0; i < itemCount; i++) {
items[i] = {

    name : prompt("Product Name"), 
    price : prompt("Product Price"), 
    qty : prompt("Product qty")

    }
}

for (i = 0; i < itemCount; i++) {
var subtotal = 0;
var total = 0;
subtotal = items[i].price * items[i].qty;
total = subtotal * 1.05;
allitems = allitems + subtotal; 
result.innerHTML += "Product: " + items[i].name + "<br/>";
result.innerHTML += "Total Qty: " +items[i].qty + "<br/>";
result.innerHTML += "Sub total: " + subtotal + "<br/>";
result.innerHTML += "Sub total: " + total + "<br/>";
if(i == (itemCount - 1)){result.innerHTML += "Sub total for all items: " + allitems + "<br/>";}
}
    }
</script>
    </head>
<body>
<button onclick="getit()">Shop</button>
<p id="result">Creating a JavaScript Object.</p>



</body>
</html>