-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
176 lines (161 loc) · 4.42 KB
/
script.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/* Add any JavaScript you need to this file. */
window.onload = function() {
//elements
let grid = document.querySelector('.grid');
let btnN = document.querySelector('#N64');
let btnP = document.querySelector('#PSX');
let btnS = document.querySelector('#Saturn');
let clr = document.querySelector('#Clear');
let header = document.querySelector('header');
//event listeners
btnN.addEventListener('click', function() {
addtable(products, grid, header, 'N64');
});
btnP.addEventListener('click', function() {
addtable(products, grid, header, 'PSX');
});
btnS.addEventListener('click', function() {
addtable(products, grid, header, 'Saturn');
});
clr.addEventListener('click', function() {
addtable(products, grid, header, 'all');
});
//product array
let products = [
{
name: 'Nintendo 64',
description: "Nintendo's iconic 64-bit cartridge based console.",
price: 5000,
category: 'N64',
imgUrl: 'images/N64.jpg'
},
{
name: 'Playstation',
description: "Sony's iconic disc based console.",
price: 5000,
category: 'PSX',
imgUrl: 'images/PSX.jpg'
},
{
name: 'Sega Saturn',
description: "Sega's cartridge based console.",
price: 5000,
category: 'Saturn',
imgUrl: 'images/Sega Saturn.jpg'
},
{
name: 'N64 Controller',
description: 'The iconic 3-pronged controller for the N64.',
price: 3000,
category: 'N64',
imgUrl: 'images/N64 Controller.jpg'
},
{
name: 'PSX Controller',
description: "The PSX's controller.",
price: 5000,
category: 'PSX',
imgUrl: 'images/PSX Controller.jpg'
},
{
name: 'Sega Saturn Controller (Mk 1)',
description: 'The original controller for Sega Saturn.',
price: 5000,
category: 'Saturn',
imgUrl: 'images/Saturn Controller.jpg'
},
{
name: 'Sega Saturn 3D Controller',
description: 'The 3D controller for Sega Saturn, with an analog stick.',
price: 5000,
category: 'Saturn',
imgUrl: 'images/Saturn 3D Controller.jpg'
},
{
name: 'N64 Expansion Pak',
description: 'Expand your worlds and juice up your N64!',
price: 2000,
category: 'N64',
imgUrl: 'images/N64 Expansion Pak.jpg'
},
{
name: 'N64 Rumble Pak',
description: 'Feel the Power and make your controller shake with power!',
price: 2000,
category: 'N64',
imgUrl: 'images/N64 Rumble Pak.jpg'
},
{
name: 'PSX Memory Card',
description: 'Save your progress. Available in 8mb',
price: 1000,
category: 'PSX',
imgUrl: 'images/PSX Memory Card.jpg'
},
{
name: 'PSX Multitap',
description: 'Expand your fun and play with 4 controllers!',
price: 2000,
category: 'PSX',
imgUrl: 'images/PSX Multitap.jpg'
},
{
name: 'Sega Saturn Controller (Mk 2)',
description: 'The revised controller for Sega Saturn.',
price: 5000,
category: 'Saturn',
imgUrl: 'images/Saturn Controller 2.jpg'
},
{
name: 'Sega Saturn Multitap',
description: 'Lets you plug in 6 controllers, or get 2 and plug in 12!.',
price: 2000,
category: 'Saturn',
imgUrl: 'images/Saturn Multitap.jpg'
}
];
//add products to table
addtable(products, grid, header, 'all');
};
//functions
function clearTable(grid) {
grid.innerHTML = '';
}
function addtable(products, grid, h, category) {
clearTable(grid);
if (category !== 'all') {
for (let i = 0; i < products.length; i++) {
if (products[i].category === category) addT(products[i]);
h.className = category;
}
} else {
for (let i = 0; i < products.length; i++) {
addT(products[i]);
}
h.className = 'null';
}
function addT(prod) {
let cell = document.createElement('div');
cell.className = 'container';
grid.appendChild(cell);
let pic = document.createElement('img');
cell.appendChild(pic);
pic.src = prod.imgUrl;
pic.alt = prod.name;
//create desc
let desc = document.createElement('div');
cell.appendChild(desc);
desc.className = 'desc';
let txt = document.createElement('div');
desc.appendChild(txt);
txt.className = 'txt';
txt.innerHTML =
prod.name +
'<br>$' +
prod.price / 100 +
'<br>' +
prod.description +
'<br>Category: ' +
prod.category;
}
}