JavaScript JavaScript Basics Making Decisions in Your Code with Conditional Statements The Conditional Challenge Solution

Clare Yeadon
Clare Yeadon
5,422 Points

Working with multiple Possible answers

Hello,

I got hung up on how to treat my quiz question answers since they were a number, for example question one (please excuse my lack of creativity) “How many legs does a dog have?” The answer could be four or 4. I know using .toUpperCase would help with the multiple answers connected to string ‘four’ But how do I put it all together with the numerical 4?

Clare Yeadon
Clare Yeadon
5,422 Points

Oh maybe this is an avance topic and I should just stick to word answers, is this possible?

1 Answer

Paul Messmer
Paul Messmer
15,022 Points

You could chain an if statement together to cover both that would look like this

const answer = (how ever you get the answer)
const loud = answer.toUpperCase();

if (answer === 4 || loud === "FOUR") {
      code to execute
}

This says if answer is equal to 4 execute the code if it is not does loud equal "FOUR" if it does execute the code if not skip the code.

Another way to write this and what I like to use is by using flags or something I can change to true if it is the right answer.

const answer = (how ever you get the answer)
const loud = answer.toUpperCase();
let isCorrectAnswer = false;

if (answer === 4 || loud === "FOUR") {
      isCorrectAnswer = true

      if (isCorrectAnswer) {
             code to execute
       }
}