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 trialErik Robles
Courses Plus Student 10,635 PointsUsing the score counter in the Ultimate quiz Challenge
I am trying to get the score to update with every correct answer. I must have my var score in the wrong place but don't know how to use it correctly yet. any help would be appreciated. Thank you
This is an example link
/*
This is a quiz to test the ability of
the program to keep score and award
a badge to the player at the end
*/
//This block of variables establishes the initial conditions of the answers
var correctAnswerOne = false;
var correctAnswerTwo = false;
var correctAnswerThree = false;
var correctAnswerFour = false;
var correctAnswerFive = false;
// This is the score counter which will keep track of the points throughout the game.
var score = 0
document.write(score);
// This is the start of the Questions
//This is the first block of code
var answerOne = prompt("What is the capital for the state of Washington?");
if (answerOne.toUpperCase() === 'OLYMPIA') {
alert("Great! You guessed right!");
if (answerOne.toUpperCase() === 'OLYMPIA') {
correctAnswerOne === true;
if (correctAnswerOne === true ) {
document.write(score += 1);
}
}
} else {
alert("OOOO! I am soooo sorry but that is not right. The right answer is Olympia");
}
// This is the second block of code (Which I haven't done yet.
var answerTwo = prompt("What is the past tense of drink");
1 Answer
Steven Parker
231,275 PointsThere are some issues with the program logic, in this section:
if (answerOne.toUpperCase() === "OLYMPIA") {
alert("Great! You guessed right!");
if (answerOne.toUpperCase() === "OLYMPIA") { // duplicate test
correctAnswerOne === true; // assignment needed here
if (correctAnswerOne === true) { // test not needed
document.write((score += 1)); // extra parentheses
}
}
}
Note that the 2nd "if" test is identical to the first, so it is not needed and can be removed. Then, on the next line there's a comparison operator ("===
") but an assignment (=
) was probably intended. Finally, the third test won't be needed once the assignment correction is made, because it only tests for the condition created by the assignment.
So that whole section can be condensed to:
if (answerOne.toUpperCase() === "OLYMPIA") {
alert("Great! You guessed right!");
correctAnswerOne = true; // only needed if you will use it later
document.write(score += 1);
}
Erik Robles
Courses Plus Student 10,635 PointsThank you so very much. This works so much better and is much cleaner than my origial code. You're a life saver. Kind regards Mr. Steven Parker.
Ednalyn De Dios
20,259 PointsEdnalyn De Dios
20,259 Pointscan you copy & paste your code here cuz the link doesn't work.