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 Solution

The Conditional Challenge : Not working ?

//score// var correct = 0;

var Number1 = prompt("How old are you "); if (Number1.toUpperCase() === "31" ){ correct+= 1 ;

} var Number2 = prompt ("where where you born"); if(Number2.toUpperCase() === "Lansdale" ) { correct+= 1; } var Number3 = prompt ("Name your favorite pet "); if (Number3.toUpperCase() === "pug" ){ correct += 1; } var Number4 = prompt ("Name your mothers middle name"); if(Number4.toUpperCase()=== "mae"){ correct+=1 ;

} var number5 = prompt ("Name your favorite color"); if(number5.toUpperCase() === "red"){ correct+=1 ; }

document.write ("<p> you got " + correct + " out of five </p>");

//output ranking //

if (correct === 5 ) {

document.write ("<p>You earned a gold crown </p>");
}

else if (correct >= 3 ){

document.write ("<p> You earned a bronze crown </p>"); }

else if (correct >= 2 ){

document.write ("<p> Keep trying </p>"); }

else (correct >= 1 ) {

document.write ("<h1> <strong> Try again !! </strong> </h1>"); }

your answers should be in upper case if you are going to use toUpperCase. I ran it on my local computer and was able to get it to work that way.

Also changed the last else to (correct >= 0), so if every answer is wrong.

2 Answers

Hi chuck kotulka,

I see that In the output ranking, in the last else you can't put a conditional there, you use else alone only when none of the conditions above become true, then the else will run. But you need to remove the condition: (correct >= 1 ), because there is no need.

Or use a else if as you did in other conditions (maybe you forgot the if?) In that that case if the user do not answer any question right., the "Try again" message will not be displayed. Unless you change the conditional to correct >= 0 instead of correct >= 1.

LaVaughn Haynes
LaVaughn Haynes
12,397 Points

You have a few problems with your code. See below:

//don't comment out "correct" variable
//var correct = 0;
var correct = 0;

var Number1 = prompt("How old are you "); 
if (Number1.toUpperCase() === "31" ){ 
    correct+= 1 ;
} 

//suggestion: give variables more appropriate names
//example: var question2. Number2.toUpperCase() is confusing
var Number2 = prompt ("where where you born"); 
if(Number2.toUpperCase() === "Lansdale" ) { 
    correct+= 1; 
} 

//problem: Number3.toUpperCase() === "pug" will never be true
//solution: use Number3.toLowercase() === "pug" OR Number3.toUpperCase() === "PUG"
var Number3 = prompt ("Name your favorite pet "); 
if (Number3.toUpperCase() === "pug" ){ 
    correct += 1; 
} 

var Number4 = prompt ("Name your mothers middle name"); 
if(Number4.toUpperCase()=== "mae"){ 
    correct+=1 ;
} 

var number5 = prompt ("Name your favorite color"); 
if(number5.toUpperCase() === "red"){ 
    correct+=1 ; 
}

document.write (" you got " + correct + " out of five ");

//output ranking //
if (correct === 5 ) {
    document.write ("You earned a gold crown ");
}else if (correct >= 3 ){
    document.write (" You earned a bronze crown "); 
}else if (correct >= 2 ){
document.write (" Keep trying "); 
//this is not valid
//}else (correct >= 1 ) {
//should be "}else{" or "}else if(test){"
}else{
    document.write (" Try again !! "); 
}