Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

295
Views
how to assign points to each option in a question javascript

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

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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
      }
    ]
  }
];
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!