I just want to assign a value for each option in the list then i can calculate the score based on the score of each question.
So I saw today an example of a html quiz app and I want to do like it but the idea is each choice has a certain score which is calculated at the end of the questions but the once I saw online was just counting the correct questions till the end of the array. Mine is assigning a certain mark for each choice that means there's no 'one correct answer' but rather multiple correct answers with different degrees of correctness. The problem is I can't assign each answer to a grade i tried it with objects but I couldn't access it.
const Data = [
{
question: "What does HTML stands for ?",
a: "Hyper Text Markup Language ",
b: "Hypo Text Markup Language",
c: "Hard Test Marking Language",
d: "High Text Marking Language",
answer: {a:2, b:3, c:5, d:4},
}
];
function nextQuestion() {
const answer = getValue();
if (answer) {
if (answer === Data[initialQuize].answer[key]) {
score=score+ Data[initialQuize].answer[value];
}
initialQuiz++;
if (initialQuize < Data.length) {
loadQuiz();
} else if (score === Data.length) {
quiz.innerHTML = `<h1> Congratulations<br/>You scored ${score}/${Data.length}</h1>`;
} else {
quiz.innerHTML = `<h1> You scored ${score}/${Data.length}`;
}
}
}
I hope I made it clear. Thanks
I'd say that answers should be an array of objects with text and points properties. I would structure it like this:
function nextQuestion() {
const answer = getValue();
if (answer && answer === Data[initialQuize].answers[key].text) {
score = score + Data[initialQuize].answers[key].points;
}
// ...
}
const Data = [{
question: "What does HTML stand for?",
answers: [{
text: "Hyper Text Markup Language ",
points: 2
},
{
text: "Hypo Text Markup Language",
points: 3
},
{
text: "Hard Test Marking Language",
points: 5
},
{
text: "High Text Marking Language",
points: 4
}
]
},
{
question: "What does XML stand for?",
answers: [{
text: "Xylophone Markup Language ",
value: 2
},
{
text: "X-ray Markup Language",
value: 3
},
{
text: "Extensible Markup Language",
value: 5
},
{
text: "Xavier's Morphine Language",
value: 4
}
]
}
];