Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

324
Vistas
How to display quiz questions with logic in prompt?

I have an array of questions that I need to display in the prompt in order. But the quiz has a logic, questions should be substituted depending on the user's answers. Each question has an ID, and each answer has a question ID that should be next. How can I do that? Right now my code prints them out one by one. How can I make logic in providing questions?

const questions = [{
    id: "1",
    question: "q1",
    answer_1: {
      text: "a1",
      next_question: "2",
    },
    answer_2: {
      text: "a2",
      next_question: "3",
    },
  },
  {
    id: "2",
    question: "q2",
    answer_1: {
      text: "a1",
      next_question: "",
    },
    answer_2: {
      text: "a2",
      next_question: "",
    },
  },
  {
    id: "3",
    question: "q3",
    answer_1: {
      text: "a1",
      next_question: "",
    },
    answer_2: {
      text: "a2",
      next_question: "4",
    },
  },
  {
    id: "4",
    question: "q4",
    answer_1: {
      text: "a1",
      next_question: "",
    },
    answer_2: {
      text: "a2",
      next_question: "",
    },
  },
];

function quiz(questionList) {
  questionList.forEach((question) => {
    prompt(
      `${question.question}`,
      `${question.answer_1.text} / ${question.answer_2.text}`
    );
  });
}

quiz(questions);

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can change the function to ask the question based on the question object passed as a parameter, then check the answer to the question and call itself with the next question.

const questions=[{id:"1",question:"q1",answer_1:{text:"a1",next_question:"2"},answer_2:{text:"a2",next_question:"3"}},{id:"2",question:"q2",answer_1:{text:"a1",next_question:""},answer_2:{text:"a2",next_question:""}},{id:"3",question:"q3",answer_1:{text:"a1",next_question:""},answer_2:{text:"a2",next_question:"4"}},{id:"4",question:"q4",answer_1:{text:"a1",next_question:""},answer_2:{text:"a2",next_question:""}}];

function quizQuestion({ question, answer_1, answer_2 }) {
    const answer = prompt(
      `${question}`,
      `${answer_1.text} / ${answer_2.text}`
    );

    let nextQuestion;
    if(answer == answer_1.text){
      nextQuestion = answer_1.next_question;
    } else if (answer == answer_2.text){
      nextQuestion = answer_2.next_question;
    } else {
      alert('Invalid answer')
    }
    
    if(nextQuestion){
      quizQuestion(questions.find(q => q.id == nextQuestion))
    }
}

quizQuestion(questions[0]);

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda