-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
170 lines (143 loc) · 5 KB
/
app.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
'use strict';
//////GLOBAL VARIABES & ARRAYS
var storeForm = document.getElementById('storeForm');
var storeTable = document.getElementById('tablejs');
var hours = ['6am', '7am', '8am', '9am', '10am', '11am', '12pm', '1pm', '2pm', '3pm', '4pm', '5pm', '6pm', '7pm'];
// var storeParams [
// ['1st and Pike', 23, 65, 6.3],
// ['Alki', 2, 16, 4.6],
// ['Capitol Hill', 20, 38, 2.3],
// ['SeaTac Airport',3, 24, 1.2],
// ['Seattle Center',11, 38, 3.7]
// ];
var allStores = [];
//Constructor
////Table Data Constructor
function Store(locationName, minCustPerHour, maxCustPerHour,avgCookiesPerCust) {
this.locationName = locationName;
this.minCustPerHour = minCustPerHour;
this.maxCustPerHour = maxCustPerHour;
this.avgCookiesPerCust = avgCookiesPerCust;
this.custPerHour = [],
this.cookiesSoldPerHour = [],
this.totalCookiesSoldPerDay = [],
allStores.push(this);
this.randCustPerHour = function() {
for (var i = 0; i < hours.length; i++) {
var result = Math.floor(Math.random() * (this.maxCustPerHour - this.minCustPerHour + 1)) + this.minCustPerHour;
this.custPerHour.push(Math.round(result))
}
},
this.calcCookiesSoldPerHour = function() {
for (var i = 0; i < hours.length; i++) {
var result = Math.ceil(this.avgCookiesPerCust * this.custPerHour[i]);
this.cookiesSoldPerHour.push(result);
}
},
this.calctotalCookiesPerDay = function (){
var sumCookiesPerDay = this.cookiesSoldPerHour.reduce(function (a,b) {
return a + b;
}, 0);
this.totalCookiesSoldPerDay.push(sumCookiesPerDay);
},
this.makeDataRow = function() {
//STRETCH GOAL ---> Add here a delete button
var trEl = document.createElement('tr');
var tdEl = document.createElement('td');
tdEl.textContent = this.locationName;
trEl.appendChild(tdEl);
for (var i = 0; i < hours.length; i++) {
tdEl = document.createElement('td');
tdEl.textContent = this.cookiesSoldPerHour[i];
trEl.appendChild(tdEl);
}
tdEl = document.createElement('td');
tdEl.textContent = this.totalCookiesSoldPerDay;
trEl.appendChild(tdEl);
storeTable.appendChild(trEl);
},
this.randCustPerHour();
this.calcCookiesSoldPerHour();
this.calctotalCookiesPerDay();
}
//STRETCH GOAL
// Comment.prototype.render = function() {
// var liEl = document.createElement('li');
// liEl.innerHTML = '<img width="100px" src="img/' + this.userName + '.jpg"> <b>' + this.userName + ': </b><em>' + this.text + '</em>';
// return liEl;
// };
new Store('1st and Pike', 23, 65, 6.3);
new Store('Alki', 2, 16, 4.6);
new Store('Capitol Hill', 20, 38, 2.3);
new Store('SeaTac Airport',3, 24, 1.2);
new Store('Seattle Center',11, 38, 3.7);
////////GLOBAL FUNCTIONS///////////////////////////
function makeHeaderRow() {
var trEl = document.createElement('tr');
var thEl = document.createElement('th');
trEl = document.createElement('tr');
thEl.textContent = '';
trEl.appendChild(thEl);
for (var i = 0; i < hours.length; i++){
thEl = document.createElement('th');
thEl.textContent = hours[i];
trEl.appendChild(thEl);
}
thEl = document.createElement('th');
thEl.textContent = 'Daily Location Total';
trEl.appendChild(thEl);
storeTable.appendChild(trEl);
}
//STRETCH GOAL
// function makeFooterRow() {
// var trEl = document.createElement('tr');
// var thEl = document.createElement('th');
// trEl = document.createElement('tr');
// thEl.textContent = '';
// trEl.appendChild(thEl);
//
// for (var i = 0; i < hours.length; i++){
// thEl = document.createElement('th');
// thEl.textContent = hours[i];
// trEl.appendChild(thEl);
// }
// thEl = document.createElement('th');
// thEl.textContent = 'Daily Location Total';
// trEl.appendChild(thEl);
//
// storeTable.appendChild(trEl);
// }
function handleAddModifyStoreDataSubmit(event) {
event.preventDefault();
//local handle variables
var storeName = event.target.locationNameInput.value;
var minCust = event.target.minCustPerHourInput.value;
var maxCust = event.target.maxCustPerHourInput.value;
var avgCookies = event.target.avgCookiesPerCustInput.value;
var newStore = new Store(storeName, minCust, maxCust, avgCookies);
//Button triggers 'nuke' & rebuild
if (event.target.button) {
newStore;
storeTable.innerHTML = '';
console.log('You just cleared the table!');
masterFunctionCall();
console.log('and then rebuilt it!');
}
event.target.locationNameInput.value = null;
event.target.minCustPerHourInput.value = null;
event.target.maxCustPerHourInput.value = null;
event.target.avgCookiesPerCustInput.value = null;
}
///////////////////////////////////////////////////Call Functions
//One Function to call them all!
function masterFunctionCall() {
//Calls Header Row (Hours of Operation)
makeHeaderRow();
//Calls Location (row) name and Cookie Sales
for (var i = 0; i < allStores.length; i++) {
allStores[i].makeDataRow();
}
}
masterFunctionCall();
////////Event Listeners
storeForm.addEventListener('submit', handleAddModifyStoreDataSubmit);