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

Hunter G
Hunter G
6,612 Points

Questions & answers are showing up as [object Object] when ran, program runs properly though?!

code = https://w.trhou.se/0jmoorm1gu

Hi all, i'm trying to replicate the quiz from lesson JavaScript Loops, Arrays and Objects - Build a Quiz Challenge, Part 2 but im using an Object instead of a Two-Dimension Array for the questions / answers.. The code is running properly except when given the prompt for the question, it's showing [object Object] instead of the actual question, along with after the quiz when it displays the correct questions / answers in the document. What am I doing wrong here?

1 Answer

Hi Hunter,

You are missing a very important portion when working with objects. Here is your current code and the missing piece.

for (var i = 0; i < questions.length; i += 1) {
  question = questions[i];
  response = parseInt(prompt(question));
  if (response === question.answer) {
    correctAnswers += 1;
    alert("That's correct!");
    correct.push(question);
  } else {
    wrong.push(question);
  } 
}

/* ========================
It should be like this below
======================== */

for (var i = 0; i < questions.length; i += 1) {
  question = questions[i].question; // you forgot to request the question from the object
  response = parseInt(prompt(question));
  console.log(response, typeof response)
  if (response === questions[i].answer) { // same here to compare response and answer
    correctAnswers += 1;
    alert("That's correct!");
    correct.push(question);
  } else {
    wrong.push(question);
  }
}

I hope this helps.

Hunter G
Hunter G
6,612 Points

im really stumped man, I ended up finding out what you added / commented in from your code but I can't think of what else im missing :( If you don't want to provide the full answer, a hint would be appreciatied :)

Hey Hunter, I had a typo in my original answer and have since edited it. Copy my answer again and try it out. That should have fixed your issue.

Let me know if that is not the case.

Hunter G
Hunter G
6,612 Points

You are awesome Chyno, thank you very much! Good eye on that typo, that made all the difference :) . . It can be confusing naming objects / variables so similarly lol.

Glad I could help! :) Keep on Coding!