[英]Create a shopping list using Javascript
您好,我正在尝试为我的 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?
}
现在作业还提到了在这个问题中我们应该如何使用循环对象和数组。 我试图在代码中添加一个数组。 非常感谢有关如何处理我的代码的一些帮助,希望我能很好地解释它以获得一些帮助。
这是所有动态的工作代码,不限于 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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.