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

183
Views
¿Cómo verificar si un objeto tiene una clave?

Tengo un cuestionario que muestra preguntas con prompt() donde cada pregunta posterior depende de la respuesta del usuario. Y cada pregunta tiene dos respuestas y una id de pregunta para mostrar a continuación. Y el código muestra la pregunta dependiendo de la respuesta.

Y aquí viene mi pregunta. No todos tendrán dos respuestas posibles. ¿Cómo puedo mostrar las opciones de respuesta en prompt dependiendo de la presencia de answer_2 . Por ejemplo, si hay answer_2 , entonces:

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

Si falta answer_2 , entonces:

 const answer = prompt(`${question}`, `${answer_1.text}`)

Aquí mi código:

 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: "", }, }, { 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: "", }, }, ]; //=== question display function quiz({ 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) { quiz(questions.find((q) => q.id == nextQuestion)); } } quiz(questions[0]);

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

0

Para preguntas sin answer_2 , obtiene un valor undefined en la función de quiz . Con eso, puede usar una declaración if (answer_2) y generar lo que quiera, así:

 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: "" } }, { 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: "" } } ]; //=== question display function quiz({ question, answer_1, answer_2 }) { let answer; if (answer_2) { answer = prompt(`${question}`, `${answer_1.text} / ${answer_2.text}`); } else { answer = prompt(`${question}`, `${answer_1.text}`); } let nextQuestion; if (answer == answer_1.text) { nextQuestion = answer_1.next_question; } else if (answer_2 && answer == answer_2.text) { nextQuestion = answer_2.next_question; } else { alert("Invalid answer"); } if (nextQuestion) { quiz(questions.find((q) => q.id == nextQuestion)); } } quiz(questions[0]);

about 4 years ago · Juan Pablo Isaza Report

0

Dado que un número dividido por uno es el mismo número, 1 puede considerarse su valor predeterminado.

 const a1 = answer_1.text; const a2 = answer_2?.text || 1;
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!