Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript Basics (Retired) Making Decisions with Conditional Statements The Conditional Challenge Solution

Adam Floyd
Adam Floyd
868 Points

vars are not updating after using '+='

Here's my code for the quiz exercise. alert ("Get ready for a simple quiz!") var firstQuestion = prompt ("What is 6x5?"); var secondQuestion = prompt ("What is the capital of England?"); var thirdQuestion = prompt ("What direction does the sun rise?"); var forthQuestion = prompt ("What is the third letter in the alphabet?"); var fifthQuestion = prompt ("What programming language am I using?"); var correctAnswers = 0; firstQuestion += parseInt(firstQuestion); if ( firstQuestion === 30 ) {
correctAnswers += 1; } secondQuestion += secondQuestion.toUpperCase(); if ( secondQuestion === "LONDON") { correctAnswers += 1; } thirdQuestion += thirdQuestion.toUpperCase(); if ( thirdQuestion === "EAST") { correctAnswers += 1; } if (forthQuestion === "c" || forthQuestion === "C") { correctAnswers += 1; } fifthQuestion += fifthQuestion.toUpperCase(); if (fifthQuestion === "JAVASCRIPT") { correctAnswers += 1; } var crowns = ("") if ( correctAnswers === 5 ) { crowns =+ "the gold"; } else if ( correctAnswers === 4 || correctAnswers === 3) { crowns += "the silver"; } else if ( correctAnswers === 2 || correctAnswers === 1) { crowns += "the bronze"; } else { crowns += "no"; } alert("You got " + correctAnswers + " out of 6 problems right and are awarded " + crowns + " crown."); console.log (secondQuestion)

My answers appear like londonLONDON or eastEAST. It takes the user input and puts it in all caps together. I'm using += to update the var to all caps but it keeps the user input. I've used += before and it's worked for me. What am I doing wrong?

1 Answer

Michael Lauridsen
Michael Lauridsen
10,321 Points

With the += you are essentially saying this:

firstQuestion = firstQuestion + firstQuestion.toUpperCase():

So you would indeed get the inputted answer followed by the answer in capital case.

You should just write the equal sign without the plus.

alert("Get ready for a simple quiz!");
var firstQuestion = prompt("What is 6x5?");
var secondQuestion = prompt("What is the capital of England?");
var thirdQuestion = prompt("What direction does the sun rise?");
var forthQuestion = prompt("What is the third letter in the alphabet?");
var fifthQuestion = prompt("What programming language am I using?");
var correctAnswers = 0;
firstQuestion = parseInt(firstQuestion);
if (firstQuestion === 30) {
    correctAnswers += 1;
}
secondQuestion = secondQuestion.toUpperCase();
if (secondQuestion === "LONDON") {
    correctAnswers += 1;
}
thirdQuestion = thirdQuestion.toUpperCase();
if (thirdQuestion === "EAST") {
    correctAnswers += 1;
}
if (forthQuestion === "c" || forthQuestion === "C") {
    correctAnswers += 1;
}
fifthQuestion = fifthQuestion.toUpperCase();
if (fifthQuestion === "JAVASCRIPT") {
    correctAnswers += 1;
}
var crowns = "";
if (correctAnswers === 5) {
    crowns = "the gold";
} else
if (correctAnswers === 4 || correctAnswers === 3) {
    crowns = "the silver";
} else if (correctAnswers === 2 || correctAnswers === 1) {
    crowns = "the bronze";
} else {
    crowns = "no";
}
alert("You got " + correctAnswers + " out of 6 problems right and are awarded " + crowns + " crown.");
console.log(secondQuestion)