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 (Retired) Making Decisions with Conditional Statements The Conditional Challenge

Justin Cox
Justin Cox
12,123 Points

Posting my solution for a review :)

Please let me know how I did. :) Instead of storing their rank in a variable, I decided to output different messages in the document.write() depending on how well the user answered.

https://codepen.io/jnxvln/pen/EJeOZd

1 Answer

Bartłomiej Ambroziak
Bartłomiej Ambroziak
7,180 Points

Hi! On the conditional you don't need put all possibilities like this:

correct > 2 && correct < 5

becasue when the first parameter is false then program go to next parameter

Also if you want tell to user how many correct answers he did, you can put "correct" value to the message. Here is my edit of your solution ;)

/*
    The Ultimate Quiz Challenge
*/

var correct = 0;
var input = '';
var message = '';

// Question 1
input = prompt('There are 24 hours in a day', 'true or false');
if (input === 'true' || input.toLowerCase() === 'yes') {
  correct += 1;
}

// Question 2
input = prompt('How many quarters make $1.00?');
if (input === '4' || input.toLowerCase() === 'four') {
  correct += 1;
}

// Question 3
input = prompt('Which family does a trumpet belong to?', 'woodwinds, brass, percussion, or strings');
if (input.toLowerCase() === 'brass') {
  correct += 1;
}

// Question 4
input = prompt('Which language is this that you are learning right now?');
if (input.toLowerCase() === 'javascript') {
 correct += 1; 
}

// Question 5
input = prompt('How many fingers are on a human hand?');
if (parseInt(input) === 5 || input.toLowerCase() === 'five') {
  correct += 1;
}

/* Rank the player based on questions answered
    Gold Crown: All correct
    Silver Crown: 3-4 correct
    Bronze Crown: 1-2 correct
    No Crown: 0 correct
*/

if (correct === 5) {
  message = "Congratulations!! ALL questions were correct, you earned the GOLD CROWN!!";
} else if (correct >= 3) {
  message = "Way to Go! You answered " + correct + " questions correctly and earned the SILVER CROWN!";
} else if (correct >= 1) {
  message = "Not too bad. You answered " + correct + " questions correctly and earned the Bronze Crown.";
} else {
  message = "You did not answer any questions correctly, and therefore did not earn a crown. Sorry, try again!";
}

// Final message letting user know how many were correct
document.write("<p>" + message + "</p>");
Justin Cox
Justin Cox
12,123 Points

Ah this is great advice! Thank you for your time and your help :)