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 trialChaz Mabry
7,839 Pointsis there anything wrong with coding it this way?
I got the code to work fine but I just never though to use a loop to build the list. is there anything wrong with the way I did this?
var questions = [
["What is the capital of California?", "SACREMENTO"],
["What county is Oakland in?", "ALAMEDA"],
["What bridge connects Oakland and San Francisco?", "THE BAY BRIDGE"]
];
var missedQuestions = [];
var correctQuestions = [];
var rightAnswers = 0;
var wrongAnswer= 0;
function print(message) {
document.write(message);
}
for (var i = 0; i < 3; i += 1) {
var answer1 = prompt(questions[i][0]);
if (answer1.toUpperCase() === questions[i][1]) {
rightAnswers += 1;
correctQuestions.push(questions[i][0])
}
else {
wrongAnswer +=1;
missedQuestions.push(questions[i][0]);
}
}
var html = "<h1>You got " + rightAnswers + " question(s) right!</h1>"
html += "<ol><li>" + correctQuestions.join('</li><li>');
html += "</li></ol><h1>You got question(s) " + wrongAnswer + " wrong. =(</h1>";
html += "<ol><li>" + missedQuestions.join('</li><li>');
print(html)
2 Answers
Lindsey Wild
8,642 PointsI did mine the same way! I don't see a reason this would be wrong, just another way of doing it :) Maybe the way he did it has some benefits, but I guess we will wait and see what other say.
Jacob Mishkin
23,118 PointsThere is only a couple of things I would change, but yes in this case a for loop works great because you can cycle through the array of questions. The only things I would change and you will learn more about it though this class is 1. change the 3 in your for loop to questions.length. this is more semantic approach to the loop and if you ever want to add to the questions array you will not need to amend the for loop. 2. they teach this in the next class but try and create a function that will create the list item elements then use that function for your answers. After that it looks good!