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

Why isn't this returning 2 correct answers?

Below is the beginning of my answer for the Conditional Challenge. Question one behaves as I expect, with Correct: 1 displaying in the browser if I answer it correctly.

However, when I answer the second question correctly, it still says "Correct: 1" in the browser. What's going on?

var correctAnswers = 0;
var question1 = prompt("What is 1 + 1?");
if (parseInt(question1) === 2) {
  correctAnswers =+ 1;
} else {
  correctAnswers;
}
document.write("<p>Correct: " + correctAnswers + "</p>");

var question2 = prompt("What is 2 + 2?");
if (parseInt(4) === 4) {
  correctAnswers =+ 1;
} else {
  correctAnswers;
}
document.write("<p>Correct: " + correctAnswers + "</p>");

Hi Margaret, just added some markdown to make your code easier to read.

2 Answers

Try swapping the increment-assignment operator from =+ to += and let me know if that works for you.

Cheers!

shouldn't the second question go like this?

var question2 = prompt("What is 2 + 2?");
if (parseInt(question2) === 4) 

Yep, that's a good catch. The challenge may just be expecting the if statement to evaluate to true. This is the benefit of having several people look at code - we may each find something different.