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

190
Visualizações
How to check if an object has a key?

I have a questionnaire which shows questions with prompt() where each subsequent question depends on the user's answer. And each question has two answers and a question id to show next. And the code shows the question depending on the answer.

And here come the my question. Not every one will have two possible answers. How can I show answer options in prompt depending on the presence of answer_2. For example, if there is answer_2, then:

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

If answer_2 is missing then:

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

Here my code:

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 Respostas
Responde à pergunta

0

For questions with no answer_2 you get undefined as value in quiz function. With that you can use an if (answer_2) statement and output what you want, like so:

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 Relatório

0

Since a number divided by one is that same number, 1 can be considered your default value.

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