-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
156 lines (134 loc) · 4.74 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
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Function that validates question
// function validateQuestion() {
// const validYesNoQuestions = [
// 'am',
// 'are',
// 'aren\'t',
// 'can',
// 'can\'t',
// 'could',
// 'couldn\'t',
// 'did',
// 'didn\'t',
// 'do',
// 'does',
// 'doesn\'t',
// 'don\'t',
// 'if', // added for a 'if..., will...?' question
// 'is',
// 'isn\'t',
// 'had',
// 'hadn\'t',
// 'has',
// 'hasn\'t',
// 'have',
// 'haven\'t',
// 'may',
// 'might',
// 'must',
// 'ought',
// 'should',
// 'shouldn\'t',
// 'was',
// 'were',
// 'weren\'t',
// 'will',
// 'won\'t',
// 'would',
// 'wouldn\'t'
// ]
// // searches inside of validYesNoQuestions
// const question = validYesNoQuestions.some();
// const i;
// for (i = 0; i < question; i++) {}
// // if else
// if (question.toLowerCase() == validYesNoQuestions.toLowerCase()) {
// clickSubmitQuestion();
// } else {
// alert('Please ask the Magic Eight Ball a valid Yes or No question');
// }
// return false;
// }
// Function that creates random answer
function clickSubmitQuestion() {
const answers = [
{ 'answer': 'It is<br>certain'},
{ 'answer': 'Without<br>a<br>doubt'},
{ 'answer': 'You may<br>rely on<br>it'},
{ 'answer': 'As I see<br>it<br>yes'},
{ 'answer': 'Most<br>likely'},
{ 'answer': 'Outlook<br>good'},
{ 'answer': 'Yes'},
{ 'answer': 'Signs<br>point to<br>yes'},
{ 'answer': 'Reply<br>hazy,<br>try again', 'direction': 'dieUp'}, //dieUp
{ 'answer': 'Ask again<br>later'},
{ 'answer': 'Better<br>not tell<br>you<br>now', 'direction': 'dieUp'}, //dieUp
{ 'answer': 'Cannot<br>predict<br>now'},
{ 'answer': 'Concentrate<br>and ask<br>again'},
{ 'answer': 'Don\'t<br>count<br>on<br>it'},
{ 'answer': 'My<br>reply<br>is<br>no'},
{ 'answer': 'My<br>sources<br>say<br>no'},
{ 'answer': 'Outlook<br>not so<br>good'},
{ 'answer': 'It is<br>decidely<br>so'},
{ 'answer': 'Yes<br>definitely', 'direction': 'dieUp'}, //dieUp
{ 'answer': 'Very<br>doubtful', 'direction': 'dieUp'} //dieUp
]
// randomAnswer takes answer and returns a random answer
const randomAnswerID = [Math.floor(Math.random() * answers.length)];
const randomAnswerObj = answers[randomAnswerID];
const randomAnswer = randomAnswerObj.answer;
console.log(randomAnswerObj);
// Creates the p for the answer
const createAnswer = document.createElement("p");
createAnswer.setAttribute("id", "answer");
createAnswer.innerHTML = `${randomAnswer}`;
console.log("p with random answer");
// Creates div for gradient background
const addBackground = document.createElement("div");
addBackground.setAttribute("id", "answer-background");
// Creates div for die
const addDie = document.createElement("div");
addDie.setAttribute("id", "get-your-answer");
// Determines orientation of die and answer
if (randomAnswerObj.direction != null && randomAnswerObj.direction == 'dieUp') {
addDie.setAttribute("class", "die-up");
console.log("addDieUP");
} else {
addDie.setAttribute("class", "die-down");
console.log("addDieDown");
}
// Appends p for answer to die div
addDie.appendChild(createAnswer);
// Adds die div into gradient background div
addBackground.appendChild(addDie);
console.log("Appends addDie to addBackground");
// Adds die with answer and background to magic eight ball page
const answerWrapper = document.getElementById("answer-wrapper");
answerWrapper.innerHTML = addBackground.outerHTML;
console.log("addBackground");
return false;
};
function clickResetButton() {
// removes answer from DOM when reset button is clicked
const parentElem = document.getElementById('answer-wrapper');
const childElem = document.getElementById('answer-background');
parentElem.removeChild(childElem);
// removes question from form input
document.getElementById('form-for-question').reset();
// changes attribute function back to askQuestion() and updates button value and html
const btnText = document.getElementById('submitQuestion');
if (btnText.value == 'Ask Another Question') {
document.getElementById('submitQuestion').setAttribute('onclick', 'askQuestion();');
btnText.value = 'Submit Your Question';
btnText.innerHTML = 'Submit Your Question';
}
};
function askQuestion() {
// changes attribute function to clickResetButton() and updates button value and html
const btnText = document.getElementById('submitQuestion');
if (btnText.value == 'Submit Your Question') {
document.getElementById('submitQuestion').setAttribute('onclick', 'clickResetButton();');
btnText.value = 'Ask Another Question';
btnText.innerHTML = 'Ask Another Question';
}
};