What I’m trying to achieve here is that, the user’s answers will be compared with the questions’ answers. It’s a quiz, basically.
I’ve tried using for loops to loop over the arrays and compare whether they’ve the same answers(in strings of course), to no avail. Obviously because of my lack of knowledge.
This is the JS code.
var questAnswers = ["A", "Bb"];
//Above - questions' answers. | | | | Below - user's answers.
var userAnswers = []
$("#q1choices div").click(function() {
$("#q1choices div").not(this).css("background-color", "dodgerblue").unbind("click");
$(this).css("background-color", "#ff3333");
userAnswers.push($(this).text());
});
$("#q2choices div").click(function() {
$("#q2choices div").not(this).css("background-color", "dodgerblue").unbind("click");
$(this).css("background-color", "#ff3333");
userAnswers.push($(this).text());
});
HTML code:
<section class="quest container">
<div class="row text-center">
<h1 id="text">Question 1</h1>
</div>
<hr id="ravenline">
<div class="row text-center">
<p>Aa or A?</p>
</div>
<div id="q1choices" class="row text-center choiceholder">
<div class="col-sm-6">
<h2>Aa</h2>
</div>
<div class="col-sm-6">
<h2>A</h2>
</div>
</div>
</section>
<section class="quest container">
<div class="row text-center">
<h1 id="text">Question 2</h1>
</div>
<hr id="ravenline">
<div class="row text-center">
<p>Bb or B?</p>
</div>
<div id="q2choices" class="row text-center choiceholder">
<div class="col-sm-6">
<h2>Bb</h2>
</div>
<div class="col-sm-6">
<h2>B</h2>
</div>
</div>
</section>
<section class="quest container">
<div class="row text-center">
<h1 id="score"></h1>
</div>
</section>
I also noticed that, when I(playing as user) click on the div that contains 2 choices, no matter which answer, the text of the answer will have a space before a comma. Like this:
abcd , efgh
Notice the space between the “abcd” and the comma?
Thanks for reading.