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 trialRylin Smith
Front End Web Development Techdegree Student 6,863 PointsI cant get the code to print what is defined in the html variable.
I can't seem to find an error in my code and am still being returned no correct or wrong questions as well as getting undefined before the correct number is given.
var questions = [
['How old are you?', 25],
['How many continents are there?', 7],
['What s the number 11?', 12]
];
var correctAnswers = 0;
var question;
var answer;
var response;
var html;
var correct = [];
var wrong = [];
function print(message) {
var outputDiv = document.getElementById('output');
outputDiv.innerHTML = message;
}
function bList(arr){
var listHTML = '<ol>';
for(var i = 0; i < arr.lenght; i += 1){
listHTML += '<li>'+ arr[i]+'</li>';
}
listHTML += '</ol>';
return listHTML;
}
for (var i = 0; i < questions.length; i+=1){
question = questions[i][0];
answer = questions[i][1];
response = parseInt(prompt(question));
if(response === answer){
correct.push(question);
correctAnswers+=1;
} else {
wrong.push(question);
}}
html += 'You got ' + correctAnswers + ' questions right!';
html += '<h2>Questions you got right:</h2>';
html += bList(correct);
html += '<h2>Questions you got wrong:</h2>';
html += bList(wrong);
print(html);
2 Answers
Steven Parker
231,268 PointsThe lack of question output is due to the spelling of "lenght" (instead of "length") in the "bList" function loop.
And the "undefined" comes from concatenating "+=
" onto a variable ("html") that has no contents. Just change the first operation to a normal assignment ("=
') to fix that.
Farai Ushe
3,258 Pointshtml += '<h2>Questions you got right:</h2>';
you are also missing some quotes here, make sure your text to be printed is wrapped like below, and not like the above highlighted
html += '<h2>'Questions you got right: '</h2>';
Steven Parker
231,268 PointsThat particular line is correct in the original code. Adding those additional quotes will cause a syntax error.