JavaScript JavaScript Basics Making Decisions in Your Code with Conditional Statements The Conditional Challenge Solution

Nas Jones
Nas Jones
7,525 Points

Code 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
MOD
Cameron Childres
Treehouse Moderator 11,694 Points

Hi 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 :)