Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

297
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda