-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExample8.html
More file actions
67 lines (48 loc) · 1.45 KB
/
Example8.html
File metadata and controls
67 lines (48 loc) · 1.45 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
56
57
58
59
60
61
62
63
64
65
66
67
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Example8</title>
</head>
<body>
<h2>Example 8</h2><br>
<h3>Total cost of items in the basket is:<p id="totalCost"></p></h3>
<button onclick="buttonclick()">Calculate amount</button>
<script>
class Basket
{
constructor()
{
this.value = [];
}
addItem(product, qty)
{
this.value.push({ product, qty });
}
getCost(prices){
let totalCost=0;
for(let item of this.value){
if (prices[item.product])
{
totalCost += prices[item.product] * item.qty;
}
}
return totalCost;
// document.getElementById("output").innerHTML = totalcost;
}
}
function buttonclick()
{
let baskets = new Basket();
baskets.addItem("apple", 2);
baskets.addItem("banana", 4);
const prices = {
apple: 2,
banana: 1.5,
};
let total = baskets.getCost(prices);
document.getElementById("totalCost").innerHTML = "$" + total.toFixed(2);
}
</script>
</body>
</html>