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 trialOsaro Igbinovia
Full Stack JavaScript Techdegree Student 15,928 PointsThe questions gotten right or wrong isn't displaying
Is there a problem with my code?
var numb; var question; var response; var gotAnswers; var answer; var answeredCorrectly = 0; var correct = []; var wrong = [];
function print(message){
var outputDiv = document.getElementById('output');
outputDiv.innerHTML = message;
}
function info(arra){ var numb = '<ol>'; for(var i; i < arra.length; i ++){ numb += '<li>' + arra[i] + '</li>'; } numb += '</ol>'; return numb; }
var test = [ ['What is force per unit area?', 'pressure'], ['How many states are in Nigeria?', '36'], ['What is the universal solvent?', 'water'], ['How many abs does a human being have?', '6'], ['What is the most expensive apple smartphone?', 'iphone x'] ];
for(var c = 0; c < test.length; c ++){ question = test[c][0]; answer = test[c][1]; response = prompt(test[c][0]); if(response === answer){ answeredCorrectly ++; correct.push(question); }else{ wrong.push(question); } }
gotAnswers = 'You answered ' + answeredCorrectly + ' question(s) correctly'; gotAnswers += '<h2>' + 'You got these questions right:' + '</h2>'; gotAnswers += info(correct); gotAnswers += '<h2>' + 'You got these questions worong:' + '</h2>'; gotAnswers += info(wrong); print(gotAnswers);
2 Answers
Steven Parker
231,210 PointsThe loop in the info function doesn't initialize the index variable:
for (var i; i < arra.length; i++) { // <-- current code
for (var i = 0; i < arra.length; i++) { // <-- with fix added
In future questions, use the instructions for code formatting in the Markdown Cheatsheet pop-up below the "Add an Answer" area. Or watch this video on code formatting.
Osaro Igbinovia
Full Stack JavaScript Techdegree Student 15,928 PointsThank you very much Steven, my program is up and running now.