JavaScript
x
34
34
1
$("#question2").hide();
2
const options = [$(".option1"), $(".option2"), $(".option3"), $(".option4")];
3
4
let randomOption = function() {
5
const texts = options
6
.map(opt => opt[0].textContent)
7
.sort(() => .5 - Math.random());
8
9
$(".option1").text(texts[0]);
10
$(".option2").text(texts[1]);
11
$(".option3").text(texts[2]);
12
$(".option4").text(texts[3]);
13
}
14
15
16
17
$("#question1 button").click(function() {
18
if ($(this).hasClass("correct")) {
19
$("#question1").fadeOut();
20
$("#question2").fadeIn();
21
} else {
22
$(this).text("X")
23
}
24
})
25
$("#question2 button").click(function() {
26
if ($(this).hasClass("correct")) {
27
$("#question2").fadeOut();
28
$("#question1").fadeIn();
29
} else {
30
$(this).text("X")
31
}
32
})
33
34
console.log(randomOption())
JavaScript
1
16
16
1
button {
2
background-color: rgb(192, 235, 179)
3
}
4
5
.container {
6
margin-top: 300px;
7
display: flex;
8
justify-content: center;
9
align-self: center;
10
}
11
12
button {
13
font-size: 50px;
14
margin: 30px;
15
width: 80px;
16
}
JavaScript
1
16
16
1
<div class="container">
2
<div id="question1">question 1
3
<button class="option1 correct">1</button>
4
<button class="option2">2</button>
5
<button class="option3">3</button>
6
<button class="option4">4</button>
7
</div>
8
<div id="question2">question 2
9
<button class="option1">1</button>
10
<button class="option2 correct">2</button>
11
<button class="option3">3</button>
12
<button class="option4">4</button>
13
</div>
14
</div>
15
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js">
16
</script>
How random option catch correspondent number ?
For example:
If question random option is (4) (2) (3) (1) arrangement
Answer should be (1),because set class=”correct” in question1 option1 but in the case (1) is X (wrong answer).
I guess it get <div>
position, so array and real number not match.
What’s wrong with my code?
Advertisement
Answer
Your are fixing correct
class on theses positions. Add correct
when generate random values:
JavaScript
1
3
1
$('#question1 button:contains("1")').addClass("correct");
2
$('#question2 button:contains("2")').addClass("correct");
3
Live example with dynamic positions:
JavaScript
1
42
42
1
$("#question2").hide();
2
const options = [$(".option1"), $(".option2"), $(".option3"), $(".option4")];
3
4
let randomOption = function() {
5
const texts = options
6
.map(opt => opt[0].textContent)
7
.sort(() => .5 - Math.random());
8
9
$(".option1").text(texts[0]);
10
$(".option2").text(texts[1]);
11
$(".option3").text(texts[2]);
12
$(".option4").text(texts[3]);
13
14
let correct1 = randomInteger(1, 4);
15
let correct2 = randomInteger(1, 4);
16
17
$('#question1 button:contains(' + correct1 + ')').addClass("correct");
18
$('#question2 button:contains(' + correct2 + ')').addClass("correct");
19
}
20
21
$("#question1 button").click(function() {
22
if ($(this).hasClass("correct")) {
23
$("#question1").fadeOut();
24
$("#question2").fadeIn();
25
} else {
26
$(this).text("X")
27
}
28
})
29
$("#question2 button").click(function() {
30
if ($(this).hasClass("correct")) {
31
$("#question2").fadeOut();
32
$("#question1").fadeIn();
33
} else {
34
$(this).text("X")
35
}
36
})
37
38
function randomInteger(min, max) {
39
return Math.floor(Math.random() * (max - min + 1)) + min;
40
}
41
42
console.log(randomOption());
JavaScript
1
16
16
1
button {
2
background-color: rgb(192, 235, 179)
3
}
4
5
.container {
6
margin-top: 300px;
7
display: flex;
8
justify-content: center;
9
align-self: center;
10
}
11
12
button {
13
font-size: 50px;
14
margin: 30px;
15
width: 80px;
16
}
JavaScript
1
15
15
1
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
2
<div class="container">
3
<div id="question1">question 1
4
<button class="option1">1</button>
5
<button class="option2">2</button>
6
<button class="option3">3</button>
7
<button class="option4">4</button>
8
</div>
9
<div id="question2">question 2
10
<button class="option1">1</button>
11
<button class="option2">2</button>
12
<button class="option3">3</button>
13
<button class="option4">4</button>
14
</div>
15
</div>
Perhaps in addition to dynamic positions for improvement you can also put dynamic responses instead of 1 for question 1 and 2 for question 2:
A simple adjustment can do it:
JavaScript
1
17
17
1
let randomOption = function() {
2
const texts = options
3
.map(opt => opt[0].textContent)
4
.sort(() => .5 - Math.random());
5
6
$(".option1").text(texts[0]);
7
$(".option2").text(texts[1]);
8
$(".option3").text(texts[2]);
9
$(".option4").text(texts[3]);
10
11
let correct1 = randomInteger(1, 4);
12
let correct2 = randomInteger(1, 4);
13
14
$('#question1 button:contains(' + correct1 + ')').addClass("correct");
15
$('#question2 button:contains(' + correct2 + ')').addClass("correct");
16
}
17
With simple helper function:
JavaScript
1
4
1
function randomInteger(min, max) {
2
return Math.floor(Math.random() * (max - min + 1)) + min;
3
}
4