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
Alex Hird
2,767 PointsinnerHTML
When attempting to print out the function to the HTML using getElementById method instead of doc.write it doesn't display. Why is that? I've read some questions and others have stated that it writes over the div so doesn't display, however how do i get past this. See code: thanks.
function print(message) { var outputDiv = document.getElementById('ouput'); outputDiv.innerHTML = message; }
for (var i = 0; i < questions.length; i += 1 ) { question = questions[i][0]; answer = questions[i][1]; response = parseInt(prompt(question)); if (response === answer) { correctAnswers +=1; }
}
html = "You got " + correctAnswers + " question(s) right."; print(html);
2 Answers
Clayton Perszyk
Treehouse Moderator 49,057 PointsAlex,
// do you have an answers variable in your code
var correctAnswers = 0;
// do you have an array of questions in your code
var questions = [['q1', 1],['q2', 2],['q3', 3],['q4', 4],['q5', 5]];
var html;
function print(message) {
// In your code you are missing a 't'; that would be a problem, if it's 'output' in the HTML
var outputDiv = document.getElementById('output');
alert(outputDiv)
outputDiv.innerHTML = message;
}
for (var i = 0; i < questions.length; i += 1 ) {
question = questions[i][0];
answer = questions[i][1];
response = parseInt(prompt(question));
if (response === answer) {
correctAnswers +=1;
}
}
html = "You got " + correctAnswers + " question(s) right.";
print(html);
Best, Clayton
P.S. Just for future reference, you probably want to format your code when you ask questions; it makes it easier for someone to help you (under the form to ask a question, there is a markdown cheatsheet to help you format correctly).
Owa Aquino
19,277 PointsHi Alex,
I could see your function variable outputDiv is calling the 'ouput' is this how you created it on your html or should it be 'output'? Your problem is occurring because you're calling a none existing class.
Try updating that and come back to me if the issue continues.
Cheers!
Owa