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 Data Using Objects Mixing and Matching Arrays and Objects

Julianna Kahn
Julianna Kahn
20,702 Points

I get prompts but they don't show questions or count how many correct

```javascript document.write('This is JavaScript') var questions = [ { question: 'How many states are in the United States?', answer: 50 }, { question: 'How many continents are there?', answer: 7 }, { question: 'How many legs does an insect have?', answer: 6 } ]; var correctAnswers = 0; var question; var answer; var response;

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

for (var i = 0; i < questions.length; i += 1) { question = questions[i].question; answer = questions[i].answer; response = prompt(question); response = parseInt(response); if (response === answer) { correctAnswers += 1; } }

html = "You got " + correctAnswers + " question(s) right." print(html);

4 Answers

Steven Parker
Steven Parker
229,644 Points

I tried this and each prompt had a question, and when it was all over the page showed how many answers had been correct.

If you are using a workspace you could post a link to a "snapshot" to allow examination of your environment in case there is some issue beyond the code itself.


Update: — So when I look at your snapshot, the code is different from what you have above. For example:

// code shown above, designed for objects:
  question = questions[i].question;
  answer = questions[i].answer;
// code in the snapshot, designed for arrays:
  question = questions[i][0];
  answer = questions[i][1];

So it looks like at one time the "questions" array might have had sub-arrays inside it, and then it was converted to have objects inside it. And the code above was changed to work with it, but the code in the snapshot is still what was used for the sub-arrays. That would explain why the above code works, but the workspace project does not.

Julianna Kahn
Julianna Kahn
20,702 Points

I don't know what it is, but this is still not working for me.

BTW, the last person that helped me said to post my code with this, ```javascript document.write('This is JavaScript') before it and that would allow it be read. I didn't quite understand it but that is what I tried doing before sending it to you. But is this what you meant? https://w.trhou.se/i3yh02k7tl? I guess I'm still a little confused.

Steven Parker
Steven Parker
229,644 Points

Yes, that's a snapshot and it allows the entire environment to be checked. And when you format pasted-in code, those three marks need to be on a line by themselves (or followed just by the language abbreviation).

See the update I added to my answer.

Julianna Kahn
Julianna Kahn
20,702 Points

Actually, the code I sent you with [i][0] and [i][1] were just temporary place holders I changed and was using to try and test my code. So I don't know what happened originally but it's working now.

One more thing. Would taking a snapshot be the same thing as copying from the address bar?

Thanks so much for your help.

Steven Parker
Steven Parker
229,644 Points

Your address bar URL is temporary. The snapshot gives you a persistent one that you can share.

Happy coding!

Julianna Kahn
Julianna Kahn
20,702 Points

You have been so helpful, thanks again.