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 trialNas Jones
7,849 PointsCode not working
https://w.trhou.se/5x6exdgdc1 ... This is a snapshot of my workspace
I don't see why it doesn't work like how i want it to. After the first question it stops executing. What can i do to fix this? Or just improve my code in general in this workspace.
1 Answer
Cameron Childres
11,820 PointsHi Haki,
Take a close look inside the conditional statement of your first question. A single equals sign is the assignment operator so the code is attempting to assign answerOne
to firstQuestion.toUpperCase()
. If you check your console you'll see this error:
Uncaught ReferenceError: Invalid left-hand side in assignment
at quiz.js:34
This is only failing because you can't assign a value to firstQuestion.toUpperCase()
. If the toUpperCase()
wasn't present here the assignment would succeed and return the string "albany", and since strings are considered truthy the statement would always run -- increasing the correct score by 1 even if the answer itself was incorrect. Be sure to use comparison operators like == or === when comparing values.
One more thing to note about your comparisons. All of your answers are stored as lower cased strings, but you're converting the user's answer to upper case before the comparison:
const answerTwo = "austin";
let secondQuestion = prompt("What is the capital of texas?");
if ( secondQuestion.toUpperCase() === answerTwo ) {
correct += 1;
}
If I input "austin" it will be converted to "AUSTIN" before being checked against "austin". This makes it impossible to receive a correct answer. To solve this you can either use the .toLowerCase()
method instead or store your answers in uppercase.
I hope this helps! Let me know if you have any questions :)