-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathquotes.js
72 lines (52 loc) · 1.74 KB
/
quotes.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
function randomSelection (object_length) {
return Math.floor(Math.random() * object_length);
}
var seasons = {
'1': [],
'2': [],
'3': [],
'4': []
};
$(document).ready(function(){
$.ajax({
type: 'GET',
url: 'quotes.json',
dataType: 'json',
success: function(data) {
for (var i = 0; i < data.length; i++) {
var str = data[i].season.split("");
var season = str[str.length - 1];
seasons[season].push(data[i]);
}
}
});
$('#getdata-button').on('click', function(){
var quoteP = $('#quote');
var detailsP = $('#details');
var season = $('select[name="season-selection"]').val();
if (season == 'all') {
season = randomSelection(Object.keys(seasons).length) + 1;
}
var seasonQuotes = seasons[season];
var entry = seasonQuotes[randomSelection(seasonQuotes.length)];
quoteP.html("");
detailsP.html("");
if ($.isArray(entry.quote)) {
// quoteP.html(entry.quote.join("</p><p>"));
var lines = [];
$.each(entry.quote, function(e, val) {
var split = val.split(":");
var speaker = '<div id="speaker">' + split[0] + ':</div>';
var line = '<div id="line">' + split[1] + '</div>';
lines.push('<div id="quote-container">' + speaker + line + '</div>');
});
// console.log(lines);
$('#quote').html(lines);
} else {
quoteP.html(entry.quote);
detailsP.html("-- " + entry.speaker);
}
$('#episode').html(entry.episode);
$('#season').html(entry.season);
});
});