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

RE: The Conditional Code Challenge - Why won't my question counter or correct answer counter function?

I'm having difficult locating where I'm going wrong in the JavaScript beginners course where we're challenged to make a question game.

I'm trying to include a question counter which tells the player how many questions remain at the end of each new prompt. I'm also trying to count the correct answers and show the player how many correct answers they have given at the end of each question.

I'm sure it's something glaringly obvious that I've not considered but I've literally been stuck on this for about an hour now!


/* This is the Conditional Challenge (capital cities). A quiz designed to ask five questions with the outcome of a score and ranking dependent on the player's answers */

var questions = 5;
var answers = 0;
var answersCorrect ="[" + answers + " answers correct]";
var questionsLeft = "[" + questions + " questions left]";

//first question//

var questionOne = prompt('What is the capital city of Spain? ' + questionsLeft);
if (questionOne.toUpperCase() === 'MADRID') {
  answers =+ 1;
alert('Congratulations. You are correct!');
  alert('You have ' + answersCorrect);
} else {
alert("Sorry. That's the wrong answer");
  alert('You have ' + answersCorrect);
}

//second question//

questions -= 1;
var questionTwo = prompt('What is the capital city of England ' + questionsLeft);
if (questionTwo.toUpperCase() === 'LONDON') {
  answers =+ 1;
alert('Congratulations. You are correct!');
  alert('You have ' + answersCorrect);

}
else {
alert("Sorry. That's the wrong answer");
  alert('You have ' + answersCorrect);
}

//third question//


questions -= 1;
var questionThree = prompt('What is the capital city of Australia? ' + questionsLeft);
if (questionThree.toUpperCase() === 'CANBERRA') {
  answers =+ 1;
alert('Congratulations. You are correct!');
  alert('You have ' + answersCorrect);
}
else {
alert("Sorry. That's the wrong answer");
  alert('You have ' + answersCorrect);
}

//fourth question//

questions -= 1;
var questionFour = prompt('What is the capital city of France? ' + questionsLeft);
if (questionFour.toUpperCase() === 'PARIS')

{
  answers =+ 1
alert('Congratulations. You are correct!');
  alert('You have ' + answersCorrect);
}

else {
alert("Sorry. That's the wrong answer");
  alert('You have ' + answersCorrect);
}

//fifth question//


questions -= 1;
var questionFive = prompt('What is the capital city of Peru? ' + questionsLeft);
if (questionFive.toUpperCase() === 'LIMA') {
  answers =+ 1;
alert('Congratulations. You are correct!');
  alert('You have ' + answersCorrect);
}

else {
alert("Sorry. That's the wrong answer");
  alert('You have ' + answersCorrect);
}

//award system//

if (answers === 5) {
alert("You won the golden crown!");
}

      else if (answers === 3 && answers === 4) {
  alert("You won the silver crown!");
      }

        else if (answers === 1 && answers === 2) {
        alert("You won the bronze crown");
        }

        else {
        alert("Sorry you didn't get a crown");
        }

2 Answers

Hi Will,

You should be using:

answers += 1;

instead of:

answers =+ 1;

Hope that helps,

Ede

Thanks! I knew it was something pretty basic I'd been doing wrong ;)