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 Loops, Arrays and Objects Tracking Multiple Items with Arrays Build a Quiz Challenge, Part 2

Peter May
Peter May
16,376 Points

Why is ".innerHTML" missing with my code

My code was working perfectly before with the document.write.

Now that I have changed it to the "ID and innerHTML" my code isn't working the same. The only thing I changed was the function. Before each time my loop would run it would print an individual message for each answer and a current score.

But now the code only prints once with the total score and and doesn't run my "if" conditions until the very end of the prompts.

Why?

current code: https://w.trhou.se/hna6axe7om

original code with doc.write:

/Q&A game designed to ask three or more questions and track score./

//Vars and basic functions var qANDa = [ ['what is the first 1st of the alphabet?', 'a', ], ['what is the 2nd letter of the alphabet?', 'b',], ['what is the 3rd letter of the alphabet?', 'c',] ]; var q_a; var score=0;

function print(message) { document.write(message); }

// Game Q&A Engine function askQuestions (ask){ for (i=0; i<ask.length; i+=1){ q_a=prompt(ask[i][0]); q_a=q_a.toLowerCase();

    if (q_a===qANDa[i][1]){

score+=1; print('<p>good boy! Your number of correct responses is ' + score + '.</p><br>');}

else { print('<p>Bad Dog! Your number of correct responses is ' + score + '.</p><br>'); } } }

//Activate the test. askQuestions(qANDa);

1 Answer

With your original code you have individual document.write statements. Each writes a line to the document. With your current code you assign the message to innerHTML. Each still takes place but is 'overwritten' with the current assignment. Both are displayed after the loop due to browser behavior. This is a common question with the following video and is addressed in the video's teacher's notes.

If you wanted to print all three messages you could concatenate message in your loop as follows:

function askQuestions (ask){
   var message = ""
   for (i=0; i<ask.length; i+=1){
      q_a=prompt(ask[i][0]);
      q_a=q_a.toLowerCase();

      if (q_a===qANDa[i][1]){
         score+=1;
         message += '<p>good boy! Your number of correct responses is ' + score + '.</p><br>';
      }
      else {
         message += '<p>Bad Dog! Your number of correct responses is ' + score + '.</p><br>';
      }
    }

print(message)

}
Peter May
Peter May
16,376 Points

I tried what you suggested , but the test still isn't working. The "quiz" asks me the first question and stops working.

I have tried changing the setting around to adding "print(message)" l to the code as well, but that didn't help.

Here is the new code with your recommended changes but it's not working. I am using "Chrome" if that makes a dif.

http://w.trhou.se/36i8qp7hhp