forked from junoProjectTwo/queenQuotes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
123 lines (109 loc) · 3.6 KB
/
app.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
import firebaseApp from "./firebase.js";
import {
get,
ref,
getDatabase,
push,
} from "https://www.gstatic.com/firebasejs/9.17.1/firebase-database.js";
const database = getDatabase(firebaseApp);
const dbRef = ref(database);
// Create an app object
const app = {};
// DOM Elements
app.quoteContainer = document.querySelector(".quoteContainer");
app.ulContainer = document.querySelector("#ulContainer");
app.hiddenImg = document.querySelector("#hiddenImg");
app.hiddenSection = document.querySelector(".hiddenMain");
app.hiddenButton = document.querySelector(".hiddenButton");
app.confirmationContainer = document.querySelector(".confirmationContainer");
app.quizSection = document.querySelector("#quizSection");
app.startGame = document.querySelector(".startGame");
app.queenArray = [];
// randomizer function to get a random queen index from the array
app.randomizer = (queenArray) => {
const randomIndex = Math.floor(Math.random() * app.queenArray.length);
// console.log(app.queenArray[randomIndex])
return app.queenArray[randomIndex];
};
app.firebaseCall = () => {
get(dbRef)
.then((snapshot) => {
if (snapshot.exists()) {
app.queenArray = snapshot.val().queens;
app.queen1 = app.randomizer(app.queenArray);
app.queen2 = app.randomizer(app.queenArray);
app.displayAnswers();
app.selectAnswer();
} else {
console.log("No data available");
}
})
.catch((error) => {
console.log(error);
})
};
app.displayAnswers = () => {
app.possibleAnswers = [app.queen1.name, app.queen2.name];
if (app.queen1.id < app.queen2.id) {
app.quoteContainer.innerHTML = app.queen1.quote;
app.hiddenImg.src = app.queen1.image_url;
app.correctAnswer = app.queen1.name;
} else {
app.quoteContainer.innerHTML = app.queen2.quote;
app.hiddenImg.src = app.queen2.image_url;
app.correctAnswer = app.queen2.name;
}
app.possibleAnswers.forEach((element) => {
const listElement = document.createElement("li");
const queenOptions = document.createTextNode(element);
listElement.appendChild(queenOptions);
listElement.classList.add("button", "answers");
app.ulContainer.appendChild(listElement)
})
};
app.selectAnswer = () => {
app.answers = app.ulContainer.children;
for (let i = 0; i < app.answers.length; i++) {
app.answers[i].addEventListener("click", (e) => {
e.preventDefault();
app.hiddenSection.style.display = "block";
app.hiddenSection.scrollIntoView({
behavior: "smooth",
});
app.ulContainer.style.pointerEvents = "none";
if (app.answers[i].innerHTML === app.correctAnswer) {
app.hiddenSection.style.display = "block";
let displayPhrase = document.createElement("p");
displayPhrase.innerText = "Condragulations. You're a winner, baby!";
app.confirmationContainer.appendChild(displayPhrase);
} else {
let displayPhrase = document.createElement("p");
displayPhrase.innerText =
"Good God, Get a Grip Girl. Try again, hunty!";
app.confirmationContainer.appendChild(displayPhrase);
}
});
}
}
// Function to run events
app.events = () => {
app.hiddenButton.addEventListener("click", function (e){
e.preventDefault();
window.location.reload();
window.location.assign("index.html#quizSection");
});
app.startGame.addEventListener("click", function (e) {
e.preventDefault();
app.quizSection.scrollIntoView({
behavior: "smooth",
});
});
};
// init function
app.init = function () {
app.events();
app.firebaseCall();
app.randomizer();
};
// call the init function
app.init();