Skip to content

Commit 54d2c0e

Browse files
committed
Page navigation and high score functionality working
1 parent 8964124 commit 54d2c0e

File tree

6 files changed

+141
-362
lines changed

6 files changed

+141
-362
lines changed

ReadMe.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ ES6 Features (arrow functions, spread operator, const and let)
44

55
JavaScript - array functions (splice, map, sort), localStorage, fetch API
66

7-
CSS - flexbox
7+
CSS - flexbox, transitions,

app.css

Lines changed: 19 additions & 145 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
/* Resets */
2-
body,
3-
html {
4-
margin: 0;
5-
padding: 0;
6-
}
72
* {
83
-webkit-box-sizing: border-box;
94
box-sizing: border-box;
5+
font-family: Arial, Helvetica, sans-serif;
6+
margin: 0;
7+
padding: 0;
8+
}
9+
10+
/* TEXT */
11+
h1,
12+
h2,
13+
h3,
14+
h4 {
15+
margin-bottom: 10px;
1016
}
1117

1218
/* Utilities */
@@ -24,35 +30,18 @@ html {
2430
.container > * {
2531
width: 100%;
2632
}
27-
.full {
28-
height: 100vh;
29-
width: 100vw;
30-
padding: 4rem;
31-
}
3233

3334
.flex-column {
34-
display: -webkit-box;
35-
display: -ms-flexbox;
3635
display: flex;
37-
-webkit-box-orient: vertical;
38-
-webkit-box-direction: normal;
39-
-ms-flex-direction: column;
4036
flex-direction: column;
4137
}
4238

4339
.flex-center {
44-
-webkit-box-pack: center;
45-
-ms-flex-pack: center;
4640
justify-content: center;
47-
-webkit-box-align: center;
48-
49-
-ms-flex-align: center;
5041
align-items: center;
5142
}
5243

5344
.justify-center {
54-
-webkit-box-pack: center;
55-
-ms-flex-pack: center;
5645
justify-content: center;
5746
}
5847

@@ -69,121 +58,6 @@ html {
6958
position: relative;
7059
}
7160

72-
/* GAME */
73-
74-
.question {
75-
text-align: center;
76-
}
77-
78-
.choice-container {
79-
display: flex;
80-
margin-bottom: 5px;
81-
border: 1px solid black;
82-
}
83-
84-
.choice-prefix {
85-
font-size: 18px;
86-
margin: 0;
87-
padding: 15px 0;
88-
width: 50px;
89-
text-align: center;
90-
border-right: 1px solid black;
91-
}
92-
93-
.choice {
94-
flex-grow: 1;
95-
font-size: 18px;
96-
margin: 0;
97-
padding: 15px;
98-
}
99-
100-
.choice-container:hover {
101-
-webkit-box-shadow: 0 4px 14px 0 rgba(0, 0, 0, 0.15);
102-
box-shadow: 0 4px 14px 0 rgba(0, 0, 0, 0.15);
103-
-webkit-transform: translateY(-2px);
104-
transform: translateY(-2px);
105-
-webkit-transition: -webkit-transform 150ms;
106-
transition: -webkit-transform 150ms;
107-
transition: transform 150ms;
108-
transition: transform 150ms, -webkit-transform 150ms;
109-
cursor: pointer;
110-
}
111-
112-
.correct {
113-
-webkit-animation: correct 1s;
114-
animation: correct 1s;
115-
}
116-
117-
.incorrect {
118-
-webkit-animation: incorrect 750ms;
119-
animation: incorrect 750ms;
120-
}
121-
122-
#hud {
123-
display: flex;
124-
justify-content: space-between;
125-
}
126-
127-
@-webkit-keyframes correct {
128-
0% {
129-
border-color: green;
130-
background-color: green;
131-
}
132-
25% {
133-
border-color: green;
134-
background-color: green;
135-
}
136-
100% {
137-
border-color: black;
138-
background-color: white;
139-
}
140-
}
141-
142-
@keyframes correct {
143-
0% {
144-
border-color: green;
145-
background-color: green;
146-
}
147-
25% {
148-
border-color: green;
149-
background-color: green;
150-
}
151-
100% {
152-
border-color: black;
153-
background-color: white;
154-
}
155-
}
156-
157-
@-webkit-keyframes incorrect {
158-
0% {
159-
border-color: red;
160-
background-color: red;
161-
}
162-
25% {
163-
border-color: red;
164-
background-color: red;
165-
}
166-
100% {
167-
border-color: black;
168-
background-color: white;
169-
}
170-
}
171-
172-
@keyframes incorrect {
173-
0% {
174-
border-color: red;
175-
background-color: red;
176-
}
177-
25% {
178-
border-color: red;
179-
background-color: red;
180-
}
181-
100% {
182-
border-color: black;
183-
background-color: white;
184-
}
185-
}
186-
18761
/* FORMS */
18862

18963
form {
@@ -207,33 +81,33 @@ select {
20781
select {
20882
display: block;
20983
}
210-
button {
84+
.btn {
21185
font-size: 16px;
212-
height: 40px;
86+
padding: 10px 0;
21387
width: 200px;
88+
text-align: center;
21489
border: 1px solid black;
21590
margin-bottom: 10px;
91+
text-decoration: none;
92+
color: black;
21693
}
21794

218-
button:hover {
95+
.btn:hover {
21996
cursor: pointer;
220-
-webkit-box-shadow: 0 4px 14px 0 rgba(0, 0, 0, 0.15);
22197
box-shadow: 0 4px 14px 0 rgba(0, 0, 0, 0.15);
222-
-webkit-transform: translateY(-2px);
22398
transform: translateY(-2px);
224-
-webkit-transition: -webkit-transform 150ms;
22599
transition: -webkit-transform 150ms;
226100
transition: transform 150ms;
227101
transition: transform 150ms, -webkit-transform 150ms;
228102
}
229103

230-
button[disabled] {
104+
.btn[disabled] {
231105
color: white;
232106
background: grey;
233107
border: none;
234108
}
235109

236-
button[disabled]:hover {
110+
.btn[disabled]:hover {
237111
cursor: not-allowed;
238112
box-shadow: none;
239113
transform: none;

app.js

Lines changed: 0 additions & 154 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,3 @@
1-
// BUTTONS
2-
const highScoresBtn = document.getElementById("highScoresBtn");
3-
const saveScoreBtn = document.getElementById("saveScoreBtn");
4-
5-
//INPUT
6-
const usernameInput = document.getElementById("username");
7-
8-
//PAGE
9-
const pages = Array.from(document.getElementsByClassName("page"));
10-
11-
//GAME Elements
12-
const question = document.getElementById("question");
13-
const choices = Array.from(document.getElementsByClassName("choice"));
14-
const scoreText = document.getElementById("score");
15-
const questionCounterText = document.getElementById("questionCounter");
16-
17-
const CORRECT_BONUS = 10;
18-
const MAX_QUESTIONS = 5;
19-
20-
let currentQuestion = {};
21-
let acceptingAnswers = true;
22-
let score = 0;
23-
let questionCounter = 0;
24-
let availableQuestions = [];
25-
//TODO: load form json file
26-
let questions = [
27-
{
28-
question: "What is the answer to question 1?",
29-
choice1: "Choice Choice 1",
30-
choice2: "Choice Choice 2",
31-
choice3: "Choice Choice 3",
32-
choice4: "Choice Choice 4",
33-
answer: 1
34-
},
35-
{
36-
question: "What is the answer to question 2?",
37-
choice1: "Choice Choice 1",
38-
choice2: "Choice Choice 2",
39-
choice3: "Choice Choice 3",
40-
choice4: "Choice Choice 4",
41-
answer: 2
42-
},
43-
{
44-
question: "What is the answer to question 3?",
45-
choice1: "Choice Choice 1",
46-
choice2: "Choice Choice 2",
47-
choice3: "Choice Choice 3",
48-
choice4: "Choice Choice 4",
49-
answer: 4
50-
}
51-
];
52-
53-
//End Screen Elements
54-
const finalScore = document.getElementById("finalScore");
55-
56-
//High Score Elements
57-
const highScoresList = document.getElementById("highScoresList");
58-
const highScores = JSON.parse(localStorage.getItem("highScores")) || [];
59-
601
//Simulated Navigation
612
navigateTo = pageName => {
623
pages.forEach(page => {
@@ -68,101 +9,6 @@ navigateTo = pageName => {
689
});
6910
};
7011

71-
//GAME Functions
72-
73-
playGame = () => {
74-
startGame();
75-
navigateTo("game");
76-
};
77-
78-
startGame = () => {
79-
questionCounter = 0;
80-
score = 0;
81-
availableQuestions = [...questions];
82-
getNewQuestion();
83-
};
84-
85-
getNewQuestion = () => {
86-
if (availableQuestions.length === 0) {
87-
//set final score text
88-
finalScore.innerHTML = score;
89-
//Go to the end page
90-
return navigateTo("end");
91-
}
92-
questionCounter++;
93-
questionCounterText.innerText = `Question ${questionCounter}/${MAX_QUESTIONS}`;
94-
const questionIndex = Math.floor(Math.random() * availableQuestions.length);
95-
currentQuestion = availableQuestions[questionIndex];
96-
question.innerHTML = currentQuestion.question;
97-
98-
choices.forEach(choice => {
99-
const number = choice.dataset["number"];
100-
choice.innerHTML = currentQuestion["choice" + number];
101-
});
102-
103-
//Remove question from available questions
104-
availableQuestions.splice(questionIndex, 1);
105-
106-
//let users answer now that question is ready
107-
acceptingAnswers = true;
108-
};
109-
110-
choices.forEach(choice => {
111-
choice.addEventListener("click", e => {
112-
//dont let the user attempt to answer until the new question is ready
113-
if (!acceptingAnswers) return;
114-
acceptingAnswers = false;
115-
const selectedChoice = e.target;
116-
const selectedAnswer = selectedChoice.dataset["number"];
117-
118-
const classToApply =
119-
selectedAnswer == currentQuestion.answer ? "correct" : "incorrect";
120-
121-
//Add the correct/incorrect animation
122-
123-
selectedChoice.parentElement.classList.add(classToApply);
124-
125-
//Increase the score
126-
incrementScore(classToApply === "correct" ? CORRECT_BONUS : 0);
127-
128-
setTimeout(() => {
129-
selectedChoice.parentElement.classList.remove(classToApply);
130-
//Load New Question
131-
getNewQuestion();
132-
}, 1000);
133-
});
134-
});
135-
136-
incrementScore = num => {
137-
score += num;
138-
scoreText.innerHTML = "Score: " + score;
139-
};
140-
141-
//HIGH SCORES
142-
143-
showHighScores = () => {
144-
highScoresList.innerHTML = highScores
145-
.map(
146-
highScore =>
147-
`<li class="high-score">${highScore.username} - ${highScore.score}</li>`
148-
)
149-
.join("");
150-
navigateTo("highScores");
151-
};
152-
153-
saveHighScore = () => {
154-
//add the score, sort the array, and splice off starting at position 5
155-
highScores.push({ score, username: usernameInput.value });
156-
highScores.sort((a, b) => b.score - a.score);
157-
highScores.splice(5);
158-
//Save to local storage for next time
159-
localStorage.setItem("highScores", JSON.stringify(highScores));
160-
};
161-
162-
usernameInput.addEventListener("keyup", () => {
163-
saveScoreBtn.disabled = !usernameInput.value;
164-
});
165-
16612
//saves score if appropriate in array of top 5 scores
16713
// saveHighScore = () => {
16814
//make a copy of existing scores

0 commit comments

Comments
 (0)