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

Mohamed Ahmed
Mohamed Ahmed
7,449 Points

I do it , That's my code for the conditional challenge

var firstQuestion = prompt("What is the capital of Texas"); // Austin
var secondQuestion = prompt("What is the capital of Ohio"); //Columbus
var thirdQuestion = prompt("What is the capital of Virginia"); //Richmond
var fourthQuestion = prompt("What is the capital of California"); //Sacramento
var fifthQuestion = prompt("What is the capital of Massachusetts"); //Boston

if (firstQuestion.toUpperCase() === "AUSTIN") {
    var firstQuestionScore = 1;
} else {
    var firstQuestionScore = 0;
}
if (secondQuestion.toUpperCase() === "COLUMBUS") {
    var secondQuestionScore = 1;
} else {
    secondQuestionScore = 0;
}
if (thirdQuestion.toUpperCase() === "RICHMOND") {
    var thirdQuestionScore = 1;
} else {
    thirdQuestionScore = 0;
}
if (fourthQuestion.toUpperCase() === "SACRAMENTO") {
    var fourthQuestionScore = 1;
} else {
    fourthQuestionScore = 0;
}
if (fifthQuestion.toUpperCase() === "BOSTON") {
    var fifthQuestionScore = 1;
} else {
    fifthQuestionScore = 0;
}

var userScore = parseInt(firstQuestionScore+secondQuestionScore+thirdQuestionScore+fourthQuestionScore+fifthQuestionScore);

if (userScore === 5) {
  document.write("You'r score is "+ userScore +" of 5 and you get the Gold Crown"); 

} else if (userScore >= 3 && userScore <= 4 ) {
  document.write("You'r score is "+ userScore +" of 5 and you get the Silver Crown"); 

} else if (userScore >= 1 && userScore <= 2) {
  document.write("You'r score is "+ userScore +" of 5 and you get the Bronze Crown");

} else {
    document.write("Sorry! You'r score is "+userScore+" There is no Crown");
}

1 Answer

Sean T. Unwin
Sean T. Unwin
28,690 Points

Looks good Mohammed.

If I was to make a suggestion to change something it would be to change your conditionals to be absolute rather than less than/equal to. If you went this route then you would also have to change the && (and) to || (or). This recommendation is strictly for code readability to be understood better. This is because those options within the conditionals are absolute numbers, meaning there is no range and therefore no need to supply a less than qualifier.

else if (userScore >= 1 && userScore <= 2) {

Suggest change to:

else if (userScore === 1 || userScore === 2) {

Also, you have a typo in your output messages - "You'r" <-- remove the apostraphe. . :)

Mohamed Ahmed
Mohamed Ahmed
7,449 Points

Thanks for that helpful suggestion.