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 trialKyle Sullivan
15,150 PointsNo matter what I do, it always says that they answered every question correctly.
Anyone mind telling me why I may be having a problem with my code? I've tried use else/else if conditional statements to count the number of answers that were correct and incorrect. Every time it leaves the number of correct answers at five but those answered wrong are numbered correctly.
//Setting the variables for the answers
var answersCorrect = 0;
var answersWrong = 0;
//Prompting the user
alert("Hello, you're about to begin a quiz!");
var question = prompt("What is the year?");
if (question === "2015") {
answersCorrect += 1;
} else if (question != "2015") {
answersWrong += 1;
}
var question2 = prompt("Which Ocean is on the West Coast?");
if (question2.toUpperCase() === "PACIFIC") {
answersCorrect += 1;
} else if (question2.toUpperCase() != "PACIFIC"){
answersWrong += 1;
}
var question3 = prompt("What is color of the sky?");
if (question3.toUpperCase() === "BLUE") {
answersCorrect += 1;
} else if (question3.toUpperCase() != "BLUE") {
answersWrong += 1;
}
var question4 = prompt("Which Ocean is located on the East Coast?");
if (question4.toUpperCase() === "ATLANTIC") {
answersCorrect += 1;
} else if (question4.toUpperCase() != "ATLANTIC") {
answersWrong += 1;
}
var question5 = prompt("Which language was this written in?");
if (question5.toUpperCase() === "JAVASCRIPT") {
answersCorrect += 1;
} else if (question5.toUpperCase() != "JAVASCRIPT") {
answersWrong += 1;
}
//crown
if (answersCorrect = 5) {
var crown = "Gold"
}else if (answersCorrect >= 3 && answersCorrect < 5) {
var crown = "Silver";
}else if (answersCorrect >= 1 && answersCorrect < 3) {
var crown = "Bronze";
}else if (answersCorrect == 0) {
var crown = "No crown! :((";
}
document.write("<p>You answered a total of " + answersCorrect + " questions correct and got wrong " + answersWrong + " and you received a " + crown + " crown </p>");
1 Answer
Chyno Deluxe
16,936 PointsJust looked over your code and the problem is your final if statement that calculates the totals. You are actually setting answersCorrect to equal 5. so you'll just want to state if there are correct answers find out how many. also you are missing a semi-colon after "gold". View Below
//crown
if (answersCorrect) {
var crown = "Gold";
}else if (answersCorrect >= 3 && answersCorrect < 5) {
var crown = "Silver";
}else if (answersCorrect >= 1 && answersCorrect < 3) {
var crown = "Bronze";
}else if (answersCorrect == 0) {
var crown = "No crown! :((";
}
Hope this helps
Kyle Sullivan
15,150 PointsMuch appreciated! I had been staring at the code for awhile and never would have noticed that equal sign. Thank you!
Julian Gutierrez
19,201 PointsJulian Gutierrez
19,201 PointsRe-formatted your question for better readability.