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

Can I please have my code reviewed before I moved on to the next video?

This is what I currently have. Everything is working perfectly fine, except for when the conditional statements run on the correctAnswers variable. I was testing each condition, but the only one really going through is the Gold Crown message. Thank you in advance for your help!

var questions = 5;
var correctAnswers = 0;

var question1 = prompt('Can you please tell me the color of an elephant? ' + '| ' + questions + ' questions left.');
if ( question1 === 'grey' || question1 === 'gray' ) {
  alert('You are correct, the color is ' + question1 + '!');
  correctAnswers += 1;
} else {
  alert('Sorry, that is not the correct answer!');
}
questions -= 1;
var question2 = prompt('Can you tell me the city or state that has the space needle? ' + '| ' + questions + ' questions left.');
if ( question2.toUpperCase() === 'SEATTLE' || question2.toUpperCase()=== 'WASHINGTON' ) {
  alert('Yes, the correct answer is ' + question2 + '!');
  correctAnswers += 1;
} else {
  alert('Sorry, that is not the correct answer!');
}
questions -= 1;
var question3 = prompt('What is the main crucial item needed for when it rains outside? ' + '| ' + questions + ' questions left.');
if ( question3.toUpperCase() === 'UMBRELLA' || question3.toUpperCase() === 'RAINCOAT' ) {
  alert('Yes, the correct answer is ' + question3 + '!');
  correctAnswers += 1;
} else {
  alert('Sorry, that is not the correct answer!');
}
questions -= 1;
var question4 = prompt('What is a popular coffee at this moment? ' + '| ' + questions + ' questions left.' );
if ( question4.toUpperCase() === 'STARBUCKS' || question4.toUpperCase() === 'COFFEEBEAN' ) {
  alert('Yes, one of the few popular coffee chains at the moment is ' + question4 + '!');
  correctAnswers += 1;
} else {
  alert('Sorry, that is not the correct answer!');
}
questions -= 1;
var question5 = prompt('What president is trying to make mexico pay for a wall it doesn\'t want? ' + '| ' + questions + ' questions left.');
if ( question5.toUpperCase() === 'TRUMP' ) {
  alert('Yes, the correct answer is ' + question5 + '!');
  correctAnswers += 1;
} else {
  alert('Sorry, that is not the correct answer... you imbecile!');
}

if (correctAnswers = 5) {
  document.write('Congratulations, you got ' + correctAnswers + ' correct! | Gold Crown.');
} else if (correctAnswers <= 3) {
  document.write('Congratulations, you got ' + correctAnswers + ' correct! | Silver Crown.');
} else if (correctAnswers <= 2) {
  document.write('Congratulations, you got ' + correctAnswers + ' correct! | Bronze Crown.');
} else if (correctAnswers = 0) {
  document.write('Sorry, you got ' + correctAnswers + ' correct.');
}
SAMUEL LAWRENCE
SAMUEL LAWRENCE
Courses Plus Student 8,447 Points

Also you provided feedback after each question, which is a better idea than what I did. I provided my feedback at the end of the challenge, which doesn't allow the user to know which questions they got correct or wrong. I will be updating my code. Thanks.

3 Answers

Steven Parker
Steven Parker
231,275 Points

A single "=" symbol is an assignment operator. It changes the variable instead of testing it. An equality comparison operator would be the double "==" symbol.

Also, be aware that your second test ("<= 3") will cover all other possible cases except for 4. You may have intended to use ">=" instead.

And unless you specifically don't want to respond to some cases, your last block could be a plain "else" with no conditional expression.

That’s the perfect answer right there then. I didn’t realize it really until after reading your response. Thank you so much Steven. I’ll take time to work on it later then ?

SAMUEL LAWRENCE
PLUS
SAMUEL LAWRENCE
Courses Plus Student 8,447 Points

Wow! interesting. It's slightly different from the way I wrote mines. I got some better ideas from the way you wrote yours. It took me 3 days to get a working program but I never looked at the solution video. I really wanted to figure it out. So happy I was able to.

I honestly re-watched a practiced a lot before I got the hang of it. But I'm glad you were able to get some ideas!

if ( answer.toUpperCase === 'RUBY') { document.writer("you are correct")}

In the conditional statement that you wrote, don't forget to add the parenthesis for the uppercase method, so it recognizes the property you're applying to the answer value. And be sure that there are no grammatical errors, since it's supposed to be document.write instead of document.writer :).

if (answer.toUpperCase() === 'RUBY') {
    document.write('You are correct.');
}