-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
67 lines (60 loc) · 1.63 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
58
59
60
61
62
63
64
65
66
67
//generate the fortune for the ball
let answers = [
//array of fortunes
'It is certain',
'It is decidedly so',
'Without a doubt',
'Yes - definitely',
'You may rely on it',
'As I see it, yes',
'Most likely',
'Outlook good',
'Yes',
'Signs point to yes',
"Don't count on it",
'My reply is no',
'My sources say no',
'Outlook not so good',
'Very doubtful',
'Reply hazy, try again',
'Ask again later',
'Better not tell you now',
'Cannot predict now',
'Focus! Ask again',
"I don't think so",
'Cannot predict now'
];
window.onload = function() {
let eight = document.getElementById('eight');
let answer = document.getElementById('answer');
let eightball = document.getElementById('eight-ball');
let question = document.getElementById('question');
eightball.addEventListener('click', function() {
if (question.value.length < 1) {
alert('You must ask something!!');
} else {
//get a random fortune message message
shake();
//call the fortune function to get your fortune only after 2sec
setTimeout(function() {
eight.innerText = '';
let num = Math.floor(Math.random() * Math.floor(answers.length));
answer.innerText = answers[num];
}, 2000);
}
});
};
function shake() {
let eightBall = document.getElementById('eight-ball');
let messageText = document.getElementById('eight');
//remove previous message if it exists
if (messageText != null) {
messageText.parentNode.removeChild(messageText);
}
//Make the ball shake by adding the css class
eightBall.classList.add('shake');
//Remove the shake class after it stops shaking
setTimeout(function() {
eightBall.classList.remove('shake');
}, 2000);
}