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

321
Views
¿Cómo mostrar preguntas de prueba con lógica en el aviso?

Tengo una serie de preguntas que necesito mostrar en el mensaje en orden. Pero el cuestionario tiene una lógica, las preguntas deben sustituirse según las respuestas del usuario. Cada pregunta tiene una ID, y cada respuesta tiene una ID de pregunta que debería ser la siguiente. ¿Cómo puedo hacer eso? En este momento, mi código los imprime uno por uno. ¿Cómo puedo hacer lógica al proporcionar preguntas?

 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 answers
Answer question

0

Puede cambiar la función para hacer la pregunta en función del objeto de pregunta pasado como parámetro, luego verificar la respuesta a la pregunta y llamarse a sí mismo con la siguiente pregunta.

 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 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!