-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjquery-basic.html
More file actions
358 lines (290 loc) · 8.72 KB
/
jquery-basic.html
File metadata and controls
358 lines (290 loc) · 8.72 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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
<html>
<head>
<title>jQuery Basic</title>
</head>
<body>
<!-- added element from lesson -->
<div>
<h3>Reviewers:</h3>
<ul class="reviewers"></ul>
</div>
<div>
<h3>Top Restaurants(Easy):</h3>
<ul class="top-restaurants easy"></ul>
</div>
<div>
<h3>Top Restaurants(Hard):</h3>
<ul class="top-restaurants hard"></ul>
</div>
<div>
<h3>Austin Restaurants:</h3>
<ul class="austin-restaurants"></ul>
</div>
<div>
<h3>Popular Movie Genres(Easy):</h3>
<ul class="popular-movie-genres easy"></ul>
</div>
<div>
<h3>Popular Movie Genres(Hard):</h3>
<ul class="popular-movie-genres hard"></ul>
</div>
<div>
<h3>Top Movie Genres:</h3>
<ul class="top-movie-genres"></ul>
</div>
<div>
<h3>Chawsome Cheeses(Easy):</h3>
<div class="cheeses easy"></div>
</div>
<div>
<h3>Chawsome Cheeses(Hard):</h3>
<div class="cheeses hard"></div>
</div>
<form class="cheese">
<h3>New Cheese Review</h3>
<!-- This errors div will be empty until the user submits a bad request -->
<div class="errors"></div>
<label>Reviewer name:</label>
<input type="text" name="reviewer" />
<label>Cheese:</label>
<input type="text" name="cheese" />
<select name="rating">
<option value="10">10</option>
<option value="9">9</option>
<option value="8">8</option>
<option value="7">7</option>
<option value="6">6</option>
<option value="5">5</option>
<option value="4">4</option>
<option value="3">3</option>
<option value="2">2</option>
<option value="1">1</option>
</select>
<br />
<textarea name="comment"></textarea>
<br />
<input type="submit" value="Submit Review" />
</form>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">
// added code from lesson
$.get("http://critics.api.mks.io/restaurants", function (reviews) {
console.log("Restaurant reviews:", reviews)
var reviewers = []
reviews.forEach(function(r) {
if (reviewers.indexOf(r.reviewer) == -1) {
reviewers.push(r.reviewer)
}
})
console.log("All reviewers:", reviewers)
renderReviewers(reviewers)
})
var renderReviewers = function (reviewers) {
var ul = $('ul.reviewers')
reviewers.forEach(function(name) {
var li = $('<li>')
li.text(name)
ul.append(li)
})
}
// My code
// Easy way
$.get("http://critics.api.mks.io/restaurants", function (reviews){
console.log("Restaurants with a rating of 8 or above:")
var topRestaurants = []
var topRestaurantsHTML = []
reviews.forEach(function(review) {
if (parseInt(review.rating) > 7){
topRestaurants.push([parseInt(review.rating), review.venue])
}
})
topRestaurants.sort(function(a,b){return b[0] - a[0]}).forEach(function(index){
topRestaurantsHTML.push("<strong>" + index[0] + "</strong> - " + index[1])
console.log(index.join(" - "))
})
console.log("--------------------")
renderTopRestaurantsEasy(topRestaurantsHTML)
renderTopRestaurantsHard(topRestaurants)
})
var renderTopRestaurantsEasy = function (reviews){
var ul = $('ul.top-restaurants.easy')
reviews.forEach(function(venue){
var li = $('<li>')
li.html(venue)
ul.append(li)
})
}
var renderTopRestaurantsHard = function (reviews){
var ul = $('ul.top-restaurants.hard')
reviews.forEach(function(venue){
var li = $('<li>')
var strong = $('<strong>')
li.append(strong)
strong.text(venue[0])
li.append(" - " + venue[1])
ul.append(li)
})
}
$.get("http://critics.api.mks.io/restaurants", function (reviews){
console.log("Austin Restaurants:")
var austinRestaurants = []
var austinRestaurantsHTML = []
reviews.forEach(function(review) {
if (review.location){
if (review.location.match(/austin/i)){
austinRestaurants.push(review.venue)
austinRestaurantsHTML.push()
}
}
})
austinRestaurants.forEach(function(index){
console.log(index)
})
console.log("--------------------")
renderAustinRestaurants(austinRestaurants)
})
var renderAustinRestaurants = function (reviews){
var ul = $('ul.austin-restaurants')
reviews.forEach(function(venue){
var li = $('<li>')
li.html(venue)
ul.append(li)
})
}
$.get("http://critics.api.mks.io/movie-genres", function (reviews){
console.log("Popular Movie Genres (Easy):")
var popularMovieGenres = {}
var array = []
var popularMovieGenresHTML = []
var sum = {}
var average = {}
reviews.forEach(function(review) {
popularMovieGenres[review.genre] || (popularMovieGenres[review.genre] = 0)
popularMovieGenres[review.genre] += 1
sum[review.genre] || (sum[review.genre] = 0)
sum[review.genre] += parseInt(review.rating)
})
for (var item in sum) {
average[item] = parseFloat(sum[item]/popularMovieGenres[item])
}
$.map(popularMovieGenres, function(value, index) {
array.push([index, value, average[index]]);
});
array.sort(function(a,b){return b[1]-a[1]}).forEach(function(index){
console.log(index[0] + " - " + index[1] + " reviews")
popularMovieGenresHTML.push(index[0] + " - <strong>" + index[2].toFixed(2) + "</strong> <i>" +index[1] + " reviews</i>")
})
console.log("--------------------")
renderPopularMovieGenresEasy(popularMovieGenresHTML)
renderPopularMovieGenresHard(array)
})
var renderPopularMovieGenresEasy = function (reviews){
var ul = $('ul.popular-movie-genres.easy')
reviews.forEach(function(genre){
var li = $('<li>')
li.html(genre)
ul.append(li)
})
}
var renderPopularMovieGenresHard = function (reviews){
var ul = $('ul.popular-movie-genres.hard')
reviews.forEach(function(genre){
var li = $('<li>')
var strong = $('<strong>')
var em = $('<em>')
li.text(genre[0] + " - ").append(strong)
strong.text(genre[2].toFixed(2) + " ")
li.append(em)
em.text(genre[1] + " reviews")
ul.append(li)
})
}
$.get("http://critics.api.mks.io/movie-genres", function (reviews){
console.log("Top Movie Genres:")
var count = {}
var sum = {}
var average = []
var topMovieGenres = []
reviews.forEach(function(review){
count[review.genre] || (count[review.genre] = 0)
count[review.genre] += 1
sum[review.genre] || (sum[review.genre] = 0)
sum[review.genre] += parseInt(review.rating)
})
for (var item in sum) {
average.push([item, parseFloat(sum[item]/count[item])])
}
average.sort(function(a,b){return b[1]-a[1]}).forEach(function(index){
console.log(index[0] + " - " + index[1].toFixed(2))
topMovieGenres.push(index[0] + " - " + index[1].toFixed(2))
})
console.log("--------------------")
renderTopMovieGenres(topMovieGenres)
})
var renderTopMovieGenres = function (reviews){
var ul = $('ul.top-movie-genres')
reviews.forEach(function(genre){
var li = $('<li>')
li.text(genre)
ul.append(li)
})
}
$.get("http://critics.api.mks.io/cheeses", function (reviews){
var cheeseReviews = []
var cheeseReviewsHard = []
reviews.forEach(function(review){
console.log(review.cheese + ": " + review.comment + " --" + review.reviewer)
cheeseReviews.push("<h3>" + review.cheese + "</h3><blockquote>" + review.comment + " <cite>--" + review.reviewer + "</cite></blockquote>")
cheeseReviewsHard.push([review.cheese, review.comment, review.reviewer])
})
console.log("--------------------")
renderCheeseReviewsEasy(cheeseReviews)
renderCheeseReviewsHard(cheeseReviewsHard)
})
var renderCheeseReviewsEasy = function (reviews){
var div1 = $('div.cheeses.easy')
reviews.forEach(function(cheese){
var div2 = $('<div class="review easy">')
div2.html(cheese)
div1.append(div2)
})
}
var renderCheeseReviewsHard = function (array){
var div3 = $('div.cheeses.hard')
array.forEach(function(index){
var div4 = $('<div class="review hard">')
var h3 = $('<h3>')
var blockquote = $('<blockquote>')
var cite = $('<cite>')
div3.append(h3)
h3.text(index[0])
div3.append(blockquote)
blockquote.text(index[1] + " -- ")
blockquote.append(cite)
cite.text(index[2])
})
}
$('form.cheese').on('submit', function (e) {
e.preventDefault()
// Delete this next line when it starts getting annoying :)
// alert("You submitted the form! (but I stopped it)")
var requestBody = {}
requestBody.reviewer = $('[name=reviewer]', this).val()
requestBody.cheese = $('[name=cheese]', this).val()
requestBody.comment = $('[name=comment]', this).val()
requestBody.rating = $('[name=rating]', this).val()
var url = "http://critics.api.mks.io/cheeses"
$('.errors', 'form').html('')
$.post(url, requestBody)
.done(function(newCheese) {
console.log("Success! Created new cheese review:", newCheese)
$('div.review:last').append("<h3>" + requestBody.cheese + "</h3><blockquote>" + requestBody.comment + " <cite>--" + requestBody.reviewer + "</cite></blockquote>")
})
.fail(function(error) {
console.log("Invalid POST request:", error.responseText)
$('.errors', 'form').append("Invalid POST request:", error.responseText)
})
})
</script>
</body>
</html>