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

Adrian Thomas
Adrian Thomas
1,572 Points

My solution. How'd I do?

Here is my solution to this challenge. Took a minute to get it. I wonder if I can refactor the code even further, especially with the question section.

// Quiz Challenge

// Correct & Incorrect score is 0
var correctAnswer = 0;
var incorrectAnswer = 0;

// Questions centered around The Walking Dead. Points are also tracked
var answer1 = prompt ("What is the name of Rick Grime's son?")

if (answer1.toLowerCase() === 'carl') {
  alert("That's right! The answer is Carl.");
  correctAnswer +=1;
} else {
  alert("Soz. That's the wrong answer!");
  incorrectAnswer +=1;
}

var answer2 = prompt ("What is the name of Daryl Dixon's brother?")

if (answer2.toLowerCase() === 'meryl') {
    alert("That's right! The answer is Meryl.");
  correctAnswer +=1;
} else {
  alert("Soz. That's the wrong answer!");
  incorrectAnswer +=1;
}

var answer3 = prompt("What town was the Governor protecting?")

if (answer3.toLowerCase() === 'woodbury') {
    alert("That's right! The answer is Woodbory.");
  correctAnswer +=1;
} else {
  alert("Soz. That's the wrong answer!");
  incorrectAnswer +=1;
}

var answer4 = prompt("What is the name of Negan's bat?")

if (answer4.toLowerCase() === 'lucille') {
  alert("That's right! The answer is Lucille.");
  correctAnswer +=1;
} else {
  alert("Soz. That's the wrong answer!");
  incorrectAnswer +=1;
}

var answer5 = prompt("What is the name of Ezekiel's pet tiger?")

  if (answer5.toLowerCase() === 'shiva') {
  alert("That's right! The answer is Shiva.");
  correctAnswer +=1;
} else {
  alert("Soz. That's the wrong answer!");
  incorrectAnswer +=1;
}


// Player results and which crown the player has earned
if (correctAnswer >= 5) {
  document.write("<p>Great! You earned a gold crown for getting all " + correctAnswer + " of the answers right! Take a bow!</p>");
} else if (correctAnswer === 4) {
  document.write("<p>Ok. Good. You got " + correctAnswer + " of the answers right and only " + incorrectAnswer + " wrong. You are now the owner of a silver crown!</p> <br />  <p>Look, we even polished it for you too!</p>");
} else if (correctAnswer === 3 || correctAnswer === 2) {
  document.write("<p>Well at least you got a crown.  A bronze one. You got " + correctAnswer + " answers right and " + incorrectAnswer + " wrong.</p> <br />  <p>Want to try again to see if you could get a higher crown?</p>")
} else {
  document.write("<p>Bummer.  You didn't make it.  Too many incorrect answers. " + incorrectAnswer + " wrong to be exact. But fear not; try this quiz again.  I have a nice crown with your name on it!</p>")
}

1 Answer

Alok Joshi
Alok Joshi
4,944 Points

Hi Adrian Thomas , Here are some of the things you could use to refactor the code.

Store all the Questions and Answers in one Object/Array however convenient. Write a function to check for answers for re-usability. Write a function to prompt a question and return the answer. And could use the returned answer to check for it using step 2. Finally, write a function to post out the final results.

Adrian Thomas
Adrian Thomas
1,572 Points

Thanks for your feedback. I'll be sure to keep this in mind.