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

I need help with iterate object, trough all properties and printing all properties out. My code looks like this,

I really need help with this, after several days of searching i still don't get it. My code looks like this now.

var allQuestions = [ { question: "What is Javascript?", choices: ["serverlanguage", "A programming language", "A song", "A fruit"], correctAnswer:1 }, { question : "Where is javascript used?", choices : ["test", "browser", "fruitbasket", "space", "home"], correctAnswer : 1 } ];

var h1 = document.getElementById('h1'); for (var prop in allQuestions) {

h1.innerHTML =  allQuestions[prop].question;
// with hi.innerHTML i get only the first question displayed,
   // with document.write i get both questions displayed
  //document.write(prop, allQuestions[prop].question); 

}

2 Answers

Example.. I can display the first question and first choiches with innerHTML...

var h1 = document.getElementById('h1');

for (var i = 0; i < allQuestions.length; i++) { h1.innerHTML = allQuestions[1].question + ", " + allQuestions[i].choices;

}

When i try again with document.write instead of innerHTML then I can display both answers and both choices like this.

for (var i = 0; i < allQuestions.length; i++) { document.write(allQuestions[1].question + ", " + allQuestions[i].choices); }

Why cant i print both with innerHTML?

there is a typo i fixed it, not sure why i had nr 1 instead of i there. It's working now