-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshopStart.js
More file actions
55 lines (49 loc) · 2.31 KB
/
shopStart.js
File metadata and controls
55 lines (49 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// several arrays were declared
var nums = [1, 2, 3, 4];
var items = ["Coke", "Kit Kat", "Bar One", "Fanta"];
var prices = [7.5, 9.5, 8.5, 7.5];
var quantities = [0, 0, 0, 0];
var totals = [0.0, 0.0, 0.0, 0.0];
//totalOrderAmt initialized to 0 so that it can keep track of the total order
var totalOrderAmt = 0;
//Add add and the remove
function add_selection(x) {
quantities[x]++;
totals[x] = prices[x] * quantities[x];
totalOrderAmt += prices[x];
display_all();
}
function remove_selection(x) {
if (quantities[x] > 0) {
quantities[x]--;
totals[x] = prices[x] * quantities[x];
totalOrderAmt -= prices[x];
display_all();
}
}
function display_all() {
var myTable = "<table><th style='width: 100px; color: red; text-align: right;'>Num</th>";
myTable += "<th style='width: 100px; color: red; text-align: right;'>Item</th>";
myTable += "<th style='width: 100px; color: red; text-align: right;'>Price</th>";
myTable += "<th style='width: 100px; color: red; text-align: right;'>Quantity</th>";
myTable += "<th style='width: 100px; color: red; text-align: right;'>Total</th>";
myTable += "<th style='width: 100px; color: red; text-align: right;'>Add</th>";
myTable += "<th style='width: 100px; color: red; text-align: right;'>Remove</th>";
for (var i = 0; i < items.length; i++) {
myTable += "<tr><td style='width: 100px; text-align: right;'>" + nums[i] + "</td>";
myTable += "<td style='width: 100px; text-align: right;'>" + items[i] + "</td>";
myTable += "<td style='width: 100px; text-align: right;'>" + prices[i] + "</td>";
myTable += "<td style='width: 100px; text-align: right;'>" + quantities[i] + "</td>";
myTable += "<td style='width: 100px; text-align: right;'>" + totals[i] + "</td>";
myTable += "<td><button onclick='add_selection(" + i + ")'>Add</button></td>";
myTable += "<td><button onclick='remove_selection(" + i + ")'>Remove</button></td>";
}
myTable += "</table>";
myTable += "<br/><br/><p>Total: " + totalOrderAmt.toFixed(2) + "</p>";
myTable += "<button onclick='checkout()'>Checkout</button>";
document.getElementById("demo").innerHTML = myTable;
}
//update the totals
window.onload = function () {
display_all();
};