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 trial

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)