-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathscript.js
32 lines (27 loc) · 1.06 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
var lines;
var randomNumber;
var lastRandomNumber;
$(document.body).ready(function () {
// load the trivia from the server
$.ajax({
url: 'AlienNames.txt'
}).done(function(content) {
// normalize the line breaks, then split into lines
lines = content.replace(/\r\n|\r/g, '\n').trim().split('\n');
// only set up the click handler if there were lines found
if (lines && lines.length) {
$('#showLine').on('click', function () {
// loop to prevent repeating the last random number
while (randomNumber === lastRandomNumber) {
randomNumber = parseInt(Math.random() * lines.length);
// check to prevent infinite loop
if (lines.length === 1) { break; }
}
// keep track of the last random number
lastRandomNumber = randomNumber;
// show the corresponding line
$('#trivia').text(lines[randomNumber]);
});
}
});
});