In: Computer Science
I'm trying to create a javascript order form that outputs the items/cost/quantity in the form of a table. I have to use a for loop to accomplish this, but I'm not sure where to place the loop & what it needs to reference
<!DOCTYPE html>
<head>
<meta charset="utf-8" />
<style media="screen">
div{
background-color: #f7df3e;
border: 1px solid black;
padding: 10px;
margin: 30px;
}
h1{
text-align: center;
}
</style>
<script type="text/javascript">
var items = [];
var quans = [];
var costs = [];
total = 0;
text = "";
function insert(){
items.push(document.getElementById('item').value);
quans.push(document.getElementById('cost').value);
costs.push(document.getElementById('quantity').value);
document.getElementById("demo1").innerHTML = "The WHOLE array:
"+items.toString();
document.getElementById("demo2").innerHTML = "The WHOLE array:
"+costs.toString();
document.getElementById("demo3").innerHTML = "The WHOLE array:
"+quans.toString();
displayReceipt();
}
function displayReceipt(){
total = 0;
text="";
text="==============================================";
text+="<br /><h2>Mini-Mart Receipt: Maura
</h2>";
text+="<ul>";
for (var i = 0; i < items.length; i++) {
text += "<li>" +
itemsArray[i] +"</li>";
}
text += "</ul>";
document.getElementById("demo2").innerHTML =
text;
}
function checkout(){
displayReceipt();
document.getElementById("demo").innerHTML = text;
}
</script>
</head>
<body>
<div>
<h1>>Mini-Mart<</h1>
<p > Enter your <strong>Item, Cost, and
Quantity</strong> information.<br>
Submit the item by clicking on <strong>Add to
Cart</strong>.<br>
Click on the <strong>Checkout</strong> button when done
entering items. </p>
<form onsubmit = "checkout()">
<input id="item" type="text" size="40"
placeholder="Item"><br><br>
<input id = "cost" type = "number" size = "20"
placeholder="Cost"><br><br>
<input id="quantity" type="number" size="20"
placeholder="Quantity"><br><br><br>
<input type="reset" value="Add to Cart"
onclick="insert()">
<input type="submit" value="Checkout"
onclick="checkout()">
</form>
<p id="demo1"></p>
<p id="demo2"></p>
<p id="demo3"></p>
</div>
</body>
</html>
HTML File :
<!DOCTYPE html>
<head>
<meta charset="utf-8" />
<!-- style is used for internal stylesheet -->
<style media="screen">
/* style rule for div */
div {
background-color: #f7df3e;
border: 1px solid black;
padding: 10px;
margin: 30px;
}
/* style rule for h1 */
h1 {
text-align: center;
}
</style>
</head>
<body>
<div>
<h1>Mini-Mart</h1>
<p> Enter your <strong>Item, Cost, and Quantity</strong> information.<br>
Submit the item by clicking on <strong>Add to Cart</strong>.<br>
Click on the <strong>Checkout</strong> button when done entering items. </p>
<form>
<input id="item" type="text" size="40" placeholder="Item"><br><br>
<input id="cost" type="number" size="20" placeholder="Cost"><br><br>
<input id="quantity" 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>
<!-- sdript is used for javascript -->
<script type="text/javascript">
//declaring array that stores items , quantity and cost
var items = [];
var quans = [];
var costs = [];
total = 0;//declaring variable to store total
text = "";
//function insert
function insert() {
items.push(document.getElementById('item').value);
quans.push(document.getElementById('cost').value);
costs.push(document.getElementById('quantity').value);
//declarin variable to create table
var table="<table border=1><tr><th>Item</th><th>Cost</th><th>Quantity</th></tr>";
//using for loop
for(var i=0;i<items.length;i++)
{
//creating row in the table
table+="<tr><td>"+items[i]+"</td><td>"+costs[i]+"</td><td>"+quans[i]+"</td></tr>";
}
//close table
table+="</table>";
document.getElementById("demo1").innerHTML =table;
}
//function display receipt
function displayReceipt() {
total = 0;
text = "";
text = "==============================================";
text += "<br /><h2>Mini-Mart Receipt: Maura </h2>";
text += "<table border=1><tr><th>Item</th><th>Cost</th><th>Quantity</th><th>ItemCost</th></tr>";
//using for loop
for (var i = 0; i < items.length; i++) {
total=total+costs[i]*quans[i];//store all items total
//create row
text += "<tr><td>"+items[i]+"</td><td>"+costs[i]+"</td><td>"+quans[i]+"</td><td>"+costs[i]*quans[i]+"</td></tr>";
}
text+="<tr><th colspan=3>Total Cost </th><th>"+total.toFixed(2)+"</th></tr>";
text += "</table>";//close table
//display table
document.getElementById("demo2").innerHTML = text;
}
//function checkout()
function checkout() {
displayReceipt();//call function
}
</script>
</body>
</html>
===========================================
Screen showing order form :
Screen when some item is added to the cart :
Screen when checkout button is clicked :