-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrocery-list.js
43 lines (38 loc) · 1.21 KB
/
grocery-list.js
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
function Item(name, color, quantity) {
this.name = name;
this.color = color;
this.quantity = quantity;
}
Item.prototype.incrementQuantity = function() {
this.quantity++;
console.log(this.quantity);
}
function List(items) {
this.items = items;
}
List.prototype.render = function() {
let element = document.createElement('div');
element.innerHTML = "<div></div>";
for (let item of this.items) {
let name = item.name;
let color = item.color;
let quantity = item.quantity;
let nextChild = document.createElement('div');
nextChild.innerHTML =
`<div class="grocery" style="color:${color};">
${name}
${quantity}
</div>`;
nextChild.onclick = () => {
item.incrementQuantity();
}
element.firstChild.insertAdjacentElement("beforeend", nextChild);
}
document.getElementById('grocery-list').insertAdjacentElement("beforeend", element);
}
window.onload = function () {
let apple_item = new Item("apple", "red", 1);
let banana_item = new Item("banana", "yellow", 2);
let items = new List([apple_item, banana_item]);
items.render();
}