Im making a website which provides users with various articles to study and they have the option to test their knowledge with a short 5 question quiz on the material in the article. I am using Express & Sequelize to achieve this, however I’ve hit a snag.
I have the questions stored in the “questions” table which holds the question, 4 answers, and a field for the correct answer and a field for the article id.
What would be the best way of going about comparing the users answer to the stored correct_answer with the method below? Or is it even possible? If it isn't possible, what route would you suggest I take? Any help is appreciated!
Controller:
exports.article_review = (req, res) => {
Questions.findAll({
limie: 5,
raw: true,
where: {
article_id: req.params.id
}
}).then(function(data) {
res.render('article-review', {
title: "Test your knowledge",
csrfToken: req.csrfToken(),
id: req.params.id,
questions: data
})
})
}
View:
<form action="/article-review/{{id}}" method="POST">
<input type="hidden" value="{{csrfToken}}" name="_csrf" />
{{#each questions}}
<h3 class="fw-bolder">{{this.question}}</h3>
<div class="form-check">
<input class="form-check-input" type="radio" value="{{this.answer1}}" name="{{this.id}}" id="question_{{this.id}}">
<label class="form-check-label" for="question_{{this.id}}">
{{this.answer1}}
</label>
</div>
<div class="form-check">
<input class="form-check-input" type="radio" value="{{this.answer2}}" name="{{this.id}}" id="question_{{this.id}}">
<label class="form-check-label" for="question_{{this.id}}">
{{this.answer2}}
</label>
</div>
<div class="form-check">
<input class="form-check-input" type="radio" value="{{this.answer3}}" name="{{this.id}}" id="question_{{this.id}}">
<label class="form-check-label" for="question_{{this.id}}">
{{this.answer3}}
</label>
</div>
<div class="form-check">
<input class="form-check-input" type="radio" value="{{this.answer4}}" name="{{this.id}}" id="question_{{this.id}}">
<label class="form-check-label" for="question_{{this.id}}">
{{this.answer4}}
</label>
</div>
{{/each}}
<button type="submit" name="submit" class="btn btn-success btn-md">Submit</button>
</form>