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 trialElena Torrey
9,156 PointsPlease, help! Only one of the correctly answered questions gets printed in the browser. What am I missing?
var quiz = [
['What is the smallest country in the world?', 'Vatican City'],
['What is the nearest planet to the sun?', 'Mercury'],
['What country is called Land of Rising Sun?', 'Japan']
]
var correctAnswers = [];
var wrongAnswers = [];
var howManyCorrect = 0;
function print(message) {
var outputDiv = document.getElementById('output');
outputDiv.innerHTML = message;
}
function finalList (array) {
var listHTML = '<ol>';
for (var i = 0; i < array.length; i += 1) {
listHTML += '<li>' + array[i] + '</li>';
listHTML += '</ol>';
return listHTML;
}
}
for (var i = 0; i < quiz.length; i += 1) {
userAnswer = prompt(quiz[i][0]);
if (userAnswer.toUpperCase() === quiz [i][1].toUpperCase()) {
correctAnswers.push(quiz[i][0]);
howManyCorrect +=1;
} else {
wrongAnswers.push(quiz[i][0]);
}
}
html = '<p>You got ' + howManyCorrect + ' questions right!</p>';
html += '<h2>You got these questions correct: </h2>'
html += finalList(correctAnswers);
html += '<h2>You got these questions wrong: </h2>';
html += finalList(wrongAnswers);
print(html);
3 Answers
rydavim
18,814 PointsYou've got your ending ordered list tag and your return statement inside your for loop, so it's only grabbing the first question and then returning it immediately without going through the rest of your array.
function finalList (array) {
var listHTML = '<ol>';
for (var i = 0; i < array.length; i += 1) {
listHTML += '<li>' + array[i] + '</li>';
listHTML += '</ol>'; // You don't want to close your ordered list until you've gone through all the items.
return listHTML; // You don't want to return until you're done going through the list items.
}
// Move them down here, after your for loop executes.
}
Just a small problem, simple to fix. Otherwise it looks good, nice job! Happy coding! :)
Kristopher Van Sant
Courses Plus Student 18,830 PointsYou're missing a semi-colon after your closing square bracket for your quiz array. Let me know if that helps!
Elena Torrey
9,156 PointsIt worked! Thank you guys!
var quiz = [
['What is the smallest country in the world?', 'Vatican City'],
['What is the nearest planet to the sun?', 'Mercury'],
['What country is called Land of Rising Sun?', 'Japan']
];
var correctAnswers = [];
var wrongAnswers = [];
var howManyCorrect = 0;
var html = ' ';
function print(message) {
var outputDiv = document.getElementById('output');
outputDiv.innerHTML = message;
}
for (var i = 0; i < quiz.length; i += 1) {
userAnswer = prompt(quiz[i][0]);
if (userAnswer.toUpperCase() === quiz [i][1].toUpperCase()) {
correctAnswers.push(quiz[i][0]);
howManyCorrect +=1;
} else {
wrongAnswers.push(quiz[i][0]);
}
}
function finalList (array) {
var listHTML = '<ol>';
for (var i = 0; i < array.length; i += 1) {
listHTML += '<li>' + array[i] + '</li>';
}
listHTML += '</ol>';
return listHTML;
}
html = '<p>You got ' + howManyCorrect + ' questions right!</p>';
html += '<h2>You got these questions correct: </h2>';
html += finalList(correctAnswers);
html += '<h2>You got these questions wrong: </h2>';
html += finalList(wrongAnswers);
print(html);