Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
My JS file:
JavaScript
var pos = 0;
var board = document.getElementById('board');
var status = document.getElementById('status');
var questn, choice, A, B, C, D, cor = 0;
var questions = [
    ["Shortcut of copy", "CTRL+A", "CTRL+S", "CTRL+C", "CTRL+Z", "C"],
    ["10+10", "20", "30", "40", "50", "A"],
    ["10+20", "20", "30", "40", "50", "B"],
    ["10+30", "20", "30", "40", "50", "C"]
];
var incorrectAnswers = [];

function DisplayQuestion() {
  board.innerHTML = "";
  status.innerHTML = "Question " + (pos + 1) + " of " + questions.length;
  questn = questions[pos][0];
  A = questions[pos][1];
  B = questions[pos][2];
  C = questions[pos][3];
  D = questions[pos][4];
  board.innerHTML += "<h3>" + questn + "</h3>";
  board.innerHTML += "<label><input type='radio' name='choices' value='A'>" + A + "</label><br>";
  board.innerHTML += "<label><input type='radio' name='choices' value='B'>" + B + "</label><br>";
  board.innerHTML += "<label><input type='radio' name='choices' value='C'>" + C + "</label><br>";
  board.innerHTML += "<label><input type='radio' name='choices' value='D'>" + D + "</label><br>";
  board.innerHTML += "<button onclick='checkAnswer()'>Submit Answer</button>";
}

function checkAnswer() {
    var choice;
    var choices = document.getElementsByName("choices");
    var choiceSelected = false;
    for (var i = 0; i < choices.length; i++) {
        if (choices[i].checked) {
            choice = choices[i].value;
            choiceSelected = true;
            break;
        }
    }
    if (!choiceSelected) {
        alert("Please select an answer before submitting.");
        return;
    } else {
        var correctAnswer = questions[pos][5];
        
        if (choice === correctAnswer) {
            cor++;
        } else {
            incorrectAnswers.push({ 
                questionIndex: pos, 
                userChoice: { letter: choice, value: questions[pos][parseInt(choice, 10)] }, 
                correctAnswer: { letter: correctAnswer, value: questions[pos][parseInt(correctAnswer, 10)] } 
            });
        }
        pos++;
        if (pos >= questions.length) {
            DisplayResult();
        } else {
            DisplayQuestion();
        }
    }
}

function DisplayResult() {
  board.innerHTML = "<h2>You got " + cor + " questions out of " + questions.length + "</h2>";
  status.innerHTML = "Quiz completed";

  for (var i = 0; i < incorrectAnswers.length; i++) {
    var questionIndex = incorrectAnswers[i].questionIndex;
    var userChoice = incorrectAnswers[i].userChoice;
    var correctAnswer = incorrectAnswers[i].correctAnswer;

    board.innerHTML += "<h3>Question: " + questions[questionIndex][0] + "</h3>";
    board.innerHTML += "Your Answer: " + userChoice.letter + " (" + userChoice.value + ")" + "<br>";
    board.innerHTML += "Correct Answer: " + correctAnswer.letter + " (" + correctAnswer.value + ")" + "<br><br>";
  }
}
The output of displayResult is as follows:
You got 2 questions out of 4
Question: Shortcut of copy
Your Answer: A (undefined)
Correct Answer: C (undefined)

Question: 10+20
Your Answer: C (undefined)
Correct Answer: B (undefined)

I want the output of actual values of Answers instead of Option values in the result.

What I have tried:

I tried by creating another function GetAnswerText:
JavaScript
function getAnswerText(choice) {
    switch (choice) {
        case "A":
            return A;
        case "B":
            return B;
        case "C":
            return C;
        case "D":
            return D;
        default:
            return "";
    }
}


But it didn't work.

Edit: Added from comments:
JavaScript
function DisplayQuestion() {
  board.innerHTML = "";
  status.innerHTML = "Question " + (pos + 1) + " of " + questions.length;
  questn = questions[pos][0];
  A = questions[pos][1];
  B = questions[pos][2];
  C = questions[pos][3];
  D = questions[pos][4];
  board.innerHTML += "<h3>" + questn + "</h3>";
  board.innerHTML += "<label><input type='radio' name='choices' value='A'>" + A + "</label><br>";
  board.innerHTML += "<label><input type='radio' name='choices' value='B'>" + B + "</label><br>";
  board.innerHTML += "<label><input type='radio' name='choices' value='C'>" + C + "</label><br>";
  board.innerHTML += "<label><input type='radio' name='choices' value='D'>" + D + "</label><br>";
  board.innerHTML += "<button onclick='checkAnswer()'>Submit Answer</button>";
}
becomes:
JavaScript
function DisplayQuestion() {
  
  status.innerHTML = "Question " + (pos + 1) + " of " + questions.length;
  
  question_ = "<h3>" + questions[pos][0] + "</h3>";
  question_ += "<label><input type='radio' name='choices' value='A'>" + questions[pos][1] + "</label><br>";
  question_ += "<label><input type='radio' name='choices' value='B'>" + questions[pos][2] + "</label><br>";
  question_ += "<label><input type='radio' name='choices' value='C'>" + questions[pos][3] + "</label><br>";
  question_ += "<label><input type='radio' name='choices' value='D'>" + questions[pos][4] + "</label><br>";
  question_ += "<button onclick='checkAnswer()'>Submit Answer</button>";
  
  board.innerHtml = question_ ;

}


var_temp = a_var_[n] ; hardcode/useless because it's only allocation adding more datas, one more memory slot used for nothing.

"question_" var is use a temporary var, to build the output html/string.
then time comes to modify the DOM by 'innerHtml' function.

question_ is declared as 'local scope' var.
Posted
Updated 30-Jan-24 22:34pm
v2
Comments
OriginalGriff 31-Jan-24 2:40am    
"It doesn't work" is probably the most useless problem report we get - and we get it a lot. It tells us nothing about what is happening, or when it happens.
So tell us what it is doing that you didn't expect, or not doing that you did.
Tell us what you did to get it to happen.
Tell us any error messages.
Use the "Improve question" widget to edit your question and provide better information.
Member 15627495 31-Jan-24 4:10am    
function DisplayQuestion() {
  board.innerHTML = "";
  status.innerHTML = "Question " + (pos + 1) + " of " + questions.length;
  questn = questions[pos][0];
  A = questions[pos][1];
  B = questions[pos][2];
  C = questions[pos][3];
  D = questions[pos][4];
  board.innerHTML += "<h3>" + questn + "</h3>";
  board.innerHTML += "<label><input type='radio' name='choices' value='A'>" + A + "</label><br>";
  board.innerHTML += "<label><input type='radio' name='choices' value='B'>" + B + "</label><br>";
  board.innerHTML += "<label><input type='radio' name='choices' value='C'>" + C + "</label><br>";
  board.innerHTML += "<label><input type='radio' name='choices' value='D'>" + D + "</label><br>";
  board.innerHTML += "<button onclick='checkAnswer()'>Submit Answer</button>";
}

becomes :
function DisplayQuestion() {
  
  status.innerHTML = "Question " + (pos + 1) + " of " + questions.length;
  
  question_ = "<h3>" + questions[pos][0] + "</h3>";
  question_ += "<label><input type='radio' name='choices' value='A'>" + questions[pos][1] + "</label><br>";
  question_ += "<label><input type='radio' name='choices' value='B'>" + questions[pos][2] + "</label><br>";
  question_ += "<label><input type='radio' name='choices' value='C'>" + questions[pos][3] + "</label><br>";
  question_ += "<label><input type='radio' name='choices' value='D'>" + questions[pos][4] + "</label><br>";
  question_ += "<button onclick='checkAnswer()'>Submit Answer</button>";
  
  board.innerHtml = question_ ;

}


var_temp = a_var_[n] ; hardcode/useless because it's only allocation adding more datas, one more memory slot used for nothing.

"question_" var is use a temporary var, to build the output html/string.
then time comes to modify the DOM by 'innerHtml' function.

question_ is declared as 'local scope' var.

1 solution

Isn't it about your reference to the question values? I am a bit unsure of your issue?

Not sure if this would work, but can you use the "ABCD" values to reduce them to "1234" as the index to your "questions[pos]". Something like -

JavaScript
userChoice: { letter: choice, value: questions[pos][choice.charCodeAt(0) - 'A'.charCodeAt(0) + 1] }


An(A) answer should be 65 - 65 + 1         = 1
A (B) answer should be 66 - 65 + 1 = 1 + 1 = 2
A (C) answer should be 67 - 65 + 1 = 2 + 1 = 3
A (D) answer should be 68 - 65 + 1 = 3 + 1 = 4

Would this calculate the "1234" needed perhaps?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900