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 trialmartin wong
4,799 Pointsdocument.write wont print
var counter = 0;
var conclusion;
var userGuess;
var answer;
var QuestionAndAnswer = [
["What is the capital of Great Britain?" , "London"],
["Which member left the boyband One Direction to pursue a solo career?" , "Zayn Malik"],
["What team won the Premier League in the 2016-17 season?" , "Chelsea"]
]
for (i = 0; i < QuestionAndAnswer.length; i+=1) {
QuestionAndAnswers = QuestionAndAnswer [i] [0];
answer = QuestionAndAnswer [i] [1];
userGuess = prompt(QuestionAndAnswers);
userguess = userGuess.toLowerCase();
if (userGuess === answer) {
correct +=1;
}
}
var html;
function print(message) {
document.write(message);
};
html = "You got" + correct + "right!.";
print(html);
The code will not print how many answers i get right!. where am i going wrong? I have looked over the video code and mine for the print() and as far as i can see, i am doing the same thing
I am super demotivated by this course and feel like i cant go anywhere. Any other resources or something to help me out?
1 Answer
Steven Parker
231,275 PointsThis code increments a variable named "correct" when the given answer matches, and also uses "correct" to print the final result, but no variable by that name was ever established.
I'd guess you meant to put "var correct = 0;
" up near the beginning of the program.
martin wong
4,799 PointsThank you very much!
Luc de Brouwer
Full Stack JavaScript Techdegree Student 17,939 PointsLuc de Brouwer
Full Stack JavaScript Techdegree Student 17,939 PointsAs far as I can see you don't handle userInput for each time you prompt an user to enter an answer, but I can be wrong