-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
58 lines (44 loc) · 1.73 KB
/
main.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
const showTop50 = () => {
let gallery = $(".gallery");
let showTop50Endpoint = "https://api.tvmaze.com/shows";
fetch(showTop50Endpoint)
.then(response => response.json())
.then(data => {
data.sort((a, b) => { //this sorts response data by rating descending
return b.rating.average - a.rating.average;
});
for (let i = 0; i < 50; i++) {
let name = data[i].name;
let image = data[i].image.medium;
let showId = data[i].id;
let $div = $("<div class='show col-4 p-3'>");
gallery.append($div);
let $img = $("<img>");
$img.attr("src", image);
$img.attr("class", showId);
$img.attr("alt", name);
$div.append($img);
let $a = $("<a>");
$a.addClass("userLink");
$a.attr("href", "./tvShow.html");
$a.attr("target", "_blank");
$a.attr("id", showId);
$a.text(name);
$div.append($a);
}
$("a").click(function () {
let showName = $(this).text();
let showId = $(this).attr("id");
localStorage.setItem("1", showName);
localStorage.setItem("2", showId);
});
$("img").click(function () {
let showName = $(this).attr("alt");
let showId = $(this).attr("class");
localStorage.setItem("1", showName);
localStorage.setItem("2", showId);
window.location.replace("tvShow.html");
});
});
}
showTop50();