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

What's wrong with my code?

Here is my workspace: https://w.trhou.se/q1b6v4efz1

I want this program to run every time the user answers a question:

if (correctAnswer = true) { finalScore += 1; alert ('That is correct. Next Question.'); } else { alert ('Sorry, that is wrong. Next question.'); }

But currently, this snippet of the program runs only after the last answer has been submitted by the user. Why is this happening? Can anyone please explain? Also, how do I correct it?

2 Answers

Well, you'll want to use more than one equals in your if statement like so

if(correctAnswer === true)

One solution is to put that code in a function, and call the function after each question.

function printResponse(corrrectAnswer) {
  if (correctAnswer = true) {
    finalScore += 1;
    alert ('That is correct. Next Question.');
  } else {
    alert ('Sorry, that is wrong. Next question.');
  }
}

printResponse(true); // correct
printResponse(false); //if they are incorrect

Another solution is to create a function that asks questions, and pass in the question and answer to that function several times

function askQuestion(question, answer) {
  var response = prompt(question);
  printResponse(response.toUpperCase() === answer); //use the function we define earlier to print the response
}

askQuestion("Are rabbits furry?", "YES"); //asks the user if rabbits are furry, and they should answer yes

And if you're really bold, see about creating an array of question and answer pairs, and using a loop to ask all the questions in that array.

Thanks, Samuel.

I was trying to do the second solution that you suggested. This is what towards the end, I wrote in the code:

if (correctAnswer = true) { finalScore += 1; alert ('That is correct. Next Question.'); } else { alert ('Sorry, that is wrong. Next question.'); }

I only skipped this part - function printResponse(corrrectAnswer) - before starting my if clause as I am not familiar with functions at this stage. But shouldn't it work just fine with the if clause as well, the way I've done it? If not, why not?

It would work if you copy and pasted that code after each question.

Code executes from top to bottom, so if you want to execute the same code multiple times after a bunch of questions, you either have to put the code after each question, or put the code in a function, can call that function.

your script executes from top to bottom, and the order of execution is the initial alert, the 5 questions, then the conditional statement that increments the score if correct. the conditional statement is only being called once, after the questions. to fix you could expand each question block a little to increment the score if correct (you already check for correctness in each question block), or put the conditional statement in a function and call it after every question, something along those lines.