I created jquery test with the help of https://codepen.io. The code was working great, but with a different result. I was looking a way where I could display the result with both question and choices, highlighting right(green) and wrong(red). Below is the code I tried like when useranswer matches correctanaswer, then highlight the div in green else red color. Could you let me know where I was going wrong with my code.
test.js
var quiz = [{
question: "How can you get the type of arguments passed to a function?",
answers: ["using typeof operator", "using getType function", "Both of the above", "None"],
correctAnswer: 0
}, {
question: "Which built-in method returns the character at the specified index?",
answers: ["characterAt()", "getCharAt()", "charAt()", "None"],
correctAnswer: 2
}]
var i = 0;
var score = 0;
$(document).ready(function () {
$('#start').on('click', function () {
$('#questions').text(quiz[i].question);
$('#zero').text(quiz[i].answers[0]);
$('#one').text(quiz[i].answers[1]);
});
});
$(document).ready(function () {
$(document).on('click', '#next', function () {
var answer = $('input[name="answers"]:checked').val();
var answerString = quiz[i].answers[answer];
$('p[class="userAnswer"][value=' + i + ']').text(answerString);
var correctAnswer = quiz[i].correctAnswer;
$('p[class="correctAnswer"][value=' + i + ']').text(quiz[i].answers[correctAnswer]);
if (answer == quiz[i].correctAnswer) {
} else {
$('tr[class="row"][name=' + i + ']').css('background', '#FE2E64');
}
if (i < 2) {
$('.choices').css('display', 'none');
$('#questions').text(quiz[i].question);
$('#zero').text(quiz[i].answers[0]);
$('#one').text(quiz[i].answers[1]);
$('.choices').show('slow');
$('input[name="answers"]').prop('checked', false);
}
});
});