In: Computer Science
Javascript array of objects: I'm trying to get the input into an array of objects, and then display the information in a table using a for loop, but I don't think my path is right
<!DOCTYPE html>
<head>
<title>Form</title>
<meta charset="utf-8" />
<style media="screen">
h1 {
text-align: center;
}
div {
background-color: #pink;
border: 2px solid green;
padding: 15px;
margin: 65px;
}
</style>
<script>
var list = [];
total = 0;
text = "";
function Product(item, quantity, costs){
this.item = item;
this.quantity = quantity;
this.cost = cost;
}
function insert() {
list.push(new
Product(document.getElementById('productItem').value,
document.getElementById('productQuantity').value),
document.getElementById('productCost').value);
var table="<table ><tr><th>Item</th><th>Cost</th><th>Quantity</th></tr>";
for(var i=0;i<list.length;i++)
{
table+="<tr><td>"+list[i].item+"</td><td>"+list[i].cost+"</td><td>"+list[i].quantity+"</td><td>"+list[i].cost*list[i].quantity+"</td></tr>";
}
table+="</table>";
document.getElementById("demo1").innerHTML =table;
}
function displayReceipt() {
total = 0;
text = "";
text = "==============================================";
text += "<br /><h2>your order</h2>";
text +=
"<table><tr><th>Item</th><th>Cost</th><th>Quantity</th><th>Total</th></tr>";
for (var i = 0; i < list.length; i++) {
total+=list[i].cost*list[i].quantity;
text +=
"<tr><td>"+list[i].item+"</td><td>"+list[i].cost+"</td><td>"+list[i].quantity+"</td><td>"+list[i].cost*list[i].quantity+"</td></tr>";
}
text+="<tr><th colspan=3>Total Cost
</th><th>"+total.toFixed(2)+"</th></tr>";
text += "</table>";
document.getElementById("demo2").innerHTML = text;
}
function checkout() {
displayReceipt();
}
</script>
</head>
<body>
<div>
<h1>market</h1>
<
<form>
Item: <input id="item" name="productItem" type="text"
placeholder="Item"><br><br>
Cost: <input id="productCost" name="productCost" type="number"
placeholder="Cost"><br><br>
Quantity: <input id="productQuantity" name="productQuantity"
type="number" size="20"
placeholder="Quantity"><br><br><br>
<input type="reset" value="Add to Cart"
onclick="insert()">
<input type="button" value="Checkout"
onclick="checkout()">
</form>
<p id="demo1"></p>
<p id="demo2"></p>
<p id="demo3"></p>
</div>
</body>
</html>
Short Summary:
**************Please upvote the answer and appreciate our time.************
Source Code:
<!DOCTYPE html>
<head>
<title>Form</title>
<meta charset="utf-8" />
<style media="screen">
h1 {
text-align: center;
}
div {
background-color: #pink;
border: 2px solid green;
padding: 15px;
margin: 65px;
}
</style>
<script>
var list = [];
total = 0;
text = "";
function Product(item, quantity, cost){
this.item = item;
this.quantity = quantity;
this.cost = cost;
}
function insert() {
list.push(new
Product(document.getElementById('productItem').value,
parseInt(document.getElementById('productQuantity').value),
parseFloat(document.getElementById('productCost').value)));
var table="<table ><tr><th>Item</th><th>Cost</th><th>Quantity</th></tr>";
for(var i=0;i<list.length;i++){
table+="<tr><td>"+list[i].item+"</td><td>"+list[i].cost+"</td><td>"+list[i].quantity+"</td><td>"+list[i].cost*list[i].quantity+"</td></tr>";
}
table+="</table>";
document.getElementById("demo1").innerHTML =table;
}
function displayReceipt() {
total = 0;
text = "";
text = "==============================================";
text += "<br /><h2>your order</h2>";
text +=
"<table><tr><th>Item</th><th>Cost</th><th>Quantity</th><th>Total</th></tr>";
for (var i = 0; i < list.length; i++) {
total+=list[i].cost*list[i].quantity;
text +=
"<tr><td>"+list[i].item+"</td><td>"+list[i].cost+"</td><td>"+list[i].quantity+"</td><td>"+list[i].cost*list[i].quantity+"</td></tr>";
}
text+="<tr><th colspan=3>Total Cost
</th><th>"+total.toFixed(2)+"</th></tr>";
text += "</table>";
document.getElementById("demo2").innerHTML = text;
}
function checkout() {
displayReceipt();
}
</script>
</head>
<body>
<div>
<h1>market</h1>
<form>
Item: <input id="productItem" name="productItem" type="text"
placeholder="Item"><br><br>
Cost: <input id="productCost" name="productCost" type="number"
placeholder="Cost"><br><br>
Quantity: <input id="productQuantity" name="productQuantity"
type="number" size="20"
placeholder="Quantity"><br><br><br>
<input type="reset" value="Add to Cart"
onclick="insert()">
<input type="button" value="Checkout"
onclick="checkout()">
</form>
<p id="demo1"></p>
<p id="demo2"></p>
<p id="demo3"></p>
</div>
</body>
</html>
**************************************************************************************
Sample Run:
**************************************************************************************
Feel free to rate the answer and comment your questions, if you have any.
Please upvote the answer and appreciate our time.
Happy Studying!!!
**************************************************************************************