-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
102 lines (77 loc) · 2.09 KB
/
index.html
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<!DOCTYPE html>
<html>
<head>
<title>Magic 8 Ball</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<script src="js/main.js"></script>
<center>
<h1>What is your question?</h1>
<form autocomplete="off">
<input type="text" id="questionText">
</br>
</br>
<!-- return false prevents the form from "submitting" and refreshing the page -->
<input type="submit" value="Submit your question" onClick="runQuestion(); return false;">
</form>
<div>
<p id="answerBox"></p>
<script type="text/javascript">
function runQuestion () {
// Takes HTML text input and defines userQuestion var value
let userQuestion = document.getElementById("questionText").value;
let randomNumber = Math.floor(Math.random() * 8);
let eightBall = '';
if (userQuestion && randomNumber === 0) {
eightBall = 'It is certain';
} else if (userQuestion && randomNumber === 1) {
eightBall = 'It is decidedly so';
} else if (userQuestion && randomNumber === 2) {
eightBall = 'Reply hazy try again';
} else if (userQuestion && randomNumber === 3) {
eightBall = 'Cannot predict now';
} else if (userQuestion && randomNumber === 4) {
eightBall = 'Do not count on it';
} else if (userQuestion && randomNumber === 5) {
eightBall = 'My sources say no';
} else if (userQuestion && randomNumber === 6) {
eightBall = 'Outlook not so good';
} else if (userQuestion && randomNumber === 7) {
eightBall = 'Signs point to yes';
}
/* Why doesn't this work but else if does?
switch (randomNumber) {
case 0:
eightBall = 'It is certain';
break;
case 1:
eightBall = 'It is decidedly so';
break;
case 2:
eightBall = 'Reply hazy try again';
break;
case 3:
eightBall = 'Cannot predict now';
break;
case 4:
eightBall = 'Do not count on it';
break;
case 5:
eightBall = 'My sources say no';
break;
case 6:
eightBall = 'Outlook not so good';
break;
case 7:
eightBall = 'Signs point to yes';
break;
*/
// Displays eightBall text into HTML
document.getElementById("answerBox").innerHTML = eightBall;
};
</script>
</div>
</center>
</body>
</html>