I have two questions:
- How can I alert the user directly after they answer one question rather than alert them three times after they answer all three questions?
- Is there a way for me to keep track of how often the user answers correctly, and give the user a total score at the end of the quiz? donβt need to give me exact code, just a little nudge for where I should be looking π
See below for code:
JavaScript
βx
36
36
1
<!DOCTYPE html>
2
β
3
<html>
4
β
5
<p id="target"></p>
6
β
7
<button id="buttonclick" type="submit">Click me</button>
8
β
9
<script>
10
β
11
var questionOne = prompt("What is 2+2?", '');
12
var questionTwo = prompt("What is 1+1?", '');
13
var questionThree = prompt("What is 3+3?",'');
14
β
15
if (questionOne = 4) {
16
alert("You got the question right!");
17
} else {
18
alert("You got the question wrong!");
19
}
20
β
21
if (questionTwo = 2) {
22
alert("You got the question right!");
23
} else {
24
alert("You got the question wrong!");
25
}
26
β
27
if (questionThree = 6) {
28
alert("You got the question right!");
29
} else {
30
alert("You got the question wrong!");
31
}
32
β
33
</script>
34
β
35
</html>
36
β
Advertisement
Answer
JavaScript
1
18
18
1
if (prompt("What is 2+2?", '') == 4) {
2
alert("You got the question right!");
3
} else {
4
alert("You got the question wrong!");
5
}
6
β
7
if (prompt("What is 1+1?", '') == 2) {
8
alert("You got the question right!");
9
} else {
10
alert("You got the question wrong!");
11
}
12
β
13
if (prompt("What is 3+3?",'') == 6) {
14
alert("You got the question right!");
15
} else {
16
alert("You got the question wrong!");
17
}
18
β
Another option is to create a function that creates the numbers for you, so u donβt have to copy-paste the prompts.
JavaScript
1
14
14
1
const ask = () => {
2
const n1 = Math.ceil(Math.random() * 100);
3
const n2 = Math.ceil(Math.random() * 100);
4
if (prompt(`What is ${n1}+${n2}?`, '') == n1 + n2) {
5
alert("You got the question right!");
6
} else {
7
alert("You got the question wrong!");
8
}
9
}
10
β
11
ask();
12
ask();
13
ask();
14
β