-
Notifications
You must be signed in to change notification settings - Fork 0
/
mathQuizChallenge.html
131 lines (104 loc) · 3.47 KB
/
mathQuizChallenge.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
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JS Test Page</title>
<link rel="stylesheet" href="css/normalize.css">
<!-- <link rel="stylesheet" href="css/style.css"> -->
</head>
<body class="style">
<div class="content" style="text-align: center; margin-top: 50px;">
<script type="text/javascript">
/*
Ask at least five questions
Keep track of the number of questions the user answered correctly
Provide a final message after the quiz letting the user know the number of questions he or she got right.
Rank the player. If the player answered all five correctly, give that player the gold crown:
3-4 is a silver crown; 1-2 correct answers is a bronze crown and 0 correct is no crown at all.
*/
var score = 0;
// answers to five questions
var answer1 = 1;
var answer2 = 2;
var answer3 = 3;
var answer4 = 4;
var answer5 = 5;
// prompts for five questions
var question1 = prompt ("What is 1 + 0?");
var question2 = prompt ("What is 1 + 1?");
var question3 = prompt ("What is 1 + 2?");
var question4 = prompt ("What is 2 + 2?");
var question5 = prompt ("What is 2 + 3?");
// conditionals to check input against answers, add 1 to score per correct answer.
// why is score += not working? It gives SyntaxError: expected expression, got '}'
// it has to be score += 1; not score +=
if ( parseInt(question1) === answer1 ) {
score += 1;
}
if ( parseInt(question2) === answer2 ) {
score += 1;
}
if ( parseInt(question3) === answer3 ) {
score += 1;
}
if ( parseInt(question4) === answer4 ) {
score += 1;
}
if ( parseInt(question5) === answer5 ) {
score += 1;
}
/* conditional to rank player if else if with or
1. 0 score = try agains
2. 1-2 score = bronze crown
3. 3-4 score = silver crown
4. 5 score = gold crown
*/
if (score === 5) {
alert ("You answered all " + score + " correctly! You win a GOLD crown!");
} else if (score === 4 || score === 3) {
alert ("Amazing! You answered " + score + " correctly! You win a SILVER crown!");
} else if (score === 2 || score === 1) {
alert ("Nice work!! You answered " + score + " correctly! You win a BRONZE crown!");
} else {
alert ("Good try! You answered " + score + " correctly. Refresh the page to try again!");
}
/* ANSWER KEY
var correct = 0;
// ask questions
var answer1 = prompt("Name a programming language that's also a gem");
if ( answer1.toUpperCase() === 'RUBY' ) {
correct += 1;
}
var answer2 = prompt("Name a programming language that's also a snake");
if ( answer2.toUpperCase() === 'PYTHON' ) {
correct += 1;
}
var answer3 = prompt("What language do you use to style web pages?");
if ( answer3.toUpperCase() === 'CSS' ) {
correct += 1;
}
var answer4 = prompt("What language do you use to build the structure of web pages?");
if ( answer4.toUpperCase() === 'HTML' ) {
correct += 1;
}
var answer5 = prompt("What language do you use to add interactivity to a web page?");
if ( answer5.toUpperCase() === 'JAVASCRIPT' ) {
correct += 1;
}
// output results
document.write("<p>You got " + correct + " out of 5 questions correct.<p>");
// output rank
if ( correct === 5 ) {
document.write("<p><strong>You earned a gold crown!</strong></p>");
} else if (correct >= 3) {
document.write("<p><strong>You earned a silver crown.</strong></p>");
} else if (correct >= 2) {
document.write("<p><strong>You earned a bronze crown.</strong></p>");
} else {
document.write("<p><strong>No crown for you. You need to study.</strong></p>");
}
*/
</script>
</div>
</body>
</html>