-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
63 lines (45 loc) · 1.91 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
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
$(document).ready(function() {
//OBJECT Magic 8-Ball
var magic8ball = {};
//PROPERTY list of answers = ['It is certain', 'It is decidedly so', 'Without a doubt', 'Yes definitely', 'Yes', 'Reply hazy try again', 'Ask again later', 'Cannot predict not', 'Don't count on it', 'No', 'My reply is no', 'Very doubtful']
magic8ball.listofAnswers = ["It is certain", "It is decidedly so", "Without a doubt", "Yes definitely", "Yes", "Reply hazy try again", "Ask again later", "Cannot predict not", "Don't count on it", "No", "My reply is no", "Very doubtful"];
//hide image when page loads
$("#answer").hide();
//FUNCTION ask question
//INPUT question
//shake 8-ball
//8-ball picks from list of answers
//OUTPUT answer
magic8ball.askQuestion = function ()
{
//random number between 0 and 1
var randomNumber = Math.random();
//random number based on length of array
var randomNumberAnswers = randomNumber * this.listofAnswers.length;
//round number down
var randomIndex = Math.floor(randomNumberAnswers);
var answer = this.listofAnswers[randomIndex];
//Answer Area
$("#answer").text( answer );
//fade answer in after hiding
$("#answer").fadeIn(4000);
//Swap 8Ball images
$("#8ball").attr("src", "https://s3.amazonaws.com/media.skillcrush.com/skillcrush/wp-content/uploads/2016/09/magic8ballAnswer.png");
console.log("What does the future hold?");
console.log(answer);
};
//hide image when button clicked
$("#answer").hide();
//Ask Me Anything Button
var onClick = function() {
var question = prompt ("Ask me anything!");
magic8ball.askQuestion(question);
//Delay between image swap
setTimeout(function() {
//Swap 8Ball images
$("#8ball").attr("src", "https://s3.amazonaws.com/media.skillcrush.com/skillcrush/wp-content/uploads/2016/09/magic8ballQuestion.png");
}, 500);
$("8ball").effect("shake");
};
$("#questionButton").click( onClick);
});