Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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

Forrest Pollard
Forrest Pollard
1,988 Points

is there a way to allow for multiple variations of the same answer to be accepted?

my code has two questions which have a number answer. I do not know how to allow for the word "EIGHT" and the number "8" to both be accepted as correct, so i just went with the typed out word. I see this as a problem because in a real program, if someone input "8" they would get the question wrong which is problematic. I wonder if there's a way to treat the variable that allowed for multiple strings to be stored in it. Thanks in advance! :) Here's my code

const answerOne = "HEAVY METAL";
const answerTwo = "EIGHT";
const answerThree = "NINTENDO";
const answerFour = "SIX";
const answerFive = "PARIS";

let rank;
let rank0 = "No crown";
let rank1 = "Bronze";
let rank2 = "Silver";
let rank3 = "Gold";
let score = 0;

document.querySelector(`main`);

let questionOne = prompt("What is objectively the best genre of music?").toUpperCase();
  if ( questionOne === answerOne ) {
    ( score += 1 )
  }

let questionTwo = prompt("How many legs do the octopus got?").toUpperCase();
  if ( questionTwo === answerTwo ) {
    ( score += 1 )
  }

let questionThree = prompt("Which company made the Mario Bros franchise?").toUpperCase();
  if ( questionThree === answerThree ) {
    ( score += 1 )
  }

let questionFour = prompt("How many strings are on a regular electric guitar?").toUpperCase();
  if ( questionFour === answerFour ) {
    ( score += 1 ) 
  }

let questionFive = prompt("What is the capital of france?").toUpperCase();
  if ( questionFive === answerFive ) {
    ( score += 1 )
  }

if ( score === 0 ) {
  rank = rank0
  } else if ( score > 0 && score < 3 ) {
    rank = rank1
    } else if ( score > 2 && score < 5 ) {
      rank = rank2 
      } else {
        rank = rank3
}

document.querySelector(`h2`).innerHTML = `<h1> Your final score is ${score} out of 5! </h1>`;
document.querySelector(`h3`).innerHTML =`<h2> Crown earned: ${rank}! </h2>`;
Forrest Pollard
Forrest Pollard
1,988 Points

Yes, that is incredibly helpful. It's always nice to remember that there are so many ways to achieve something with code. I was more hoping that there would be some way to declare the variable as either EIGHT or (||) 8 because i have this weird concept in my head that i cant seem to override that fewer lines is "better". However, this works perfectly and is only one extra line.

Thanks :)

if ( score === 0 ) { rank = rank0 } else if ( score == 1 || score == 2) { rank = rank1 } else if ( score == 3 || score == 4 ) { rank = rank2 } else { rank = rank3 }

1 Answer

Shawn Denham
seal-mask
PLUS
.a{fill-rule:evenodd;}techdegree
Shawn Denham
Python Development Techdegree Student 17,802 Points

It's been awhile but if I remember correct you can OR your IF statement. But what I am thinking of you would have to change your logic.....

So take the input and convert it to a string and store it in an variable (answer in this case).

Then run it against the || (or) in your if statement (consider the below pseudocode...you know, just getting the idea out. I have not written JavaScript in MANY moons so...just get the jist of what I am trying to say below). :)

if ( answer == "eight" || answer == "EIGHT" || answer == "8") {
  output = Your Right!
} else {
  output = Your Wrong!
}

or.......OR! you could do the same with what you have by just having multiple answers......so....

const answerTwoA = "EIGHT";
const answerTwoB = "eight";
const answerTwoC = "8";       

THEN!!

if ( answerTwoA || answerTwoB || answerTwoC ) {
  output = Your Right!
} else {
  output = Your Wrong!
}

Anyway, not sure if that helped you at all but it was a fun thought experiment on my end! Thanks for asking the questions!!