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 trialOwen Boochever
14,582 PointsHi guys! Having difficulty getting my browser to "prompt". Is my code off, or is there something else going on?
I know this isn't the right code for the video, just illustrating what I'm working with. The loop is not working, nor are either of the prompts outside of it. Thank you!!
var questions = [
['q1', 'answer1'],
['q2', 'answer2'],
['q3', 'answer3']
];
var correctAnswers = 0;
var question;
var answer;
var response;
var html;
function print(message) {
document.write(message);
}
for (i = 0; i < questions.length; i+= 1) {
question = questions([i][0]);
answer = questions([i][1]);
response = prompt(question);
}
prompt(questions[0][0]);
prompt("hi");
3 Answers
mikes02
Courses Plus Student 16,968 PointsYou're probably seeing errors because your i variable is undefined. You should have it as:
for (var i = 0; i < questions.length; i+= 1)
Owen Boochever
14,582 PointsGot it! Had another issue with my code that didn't copy. Thank you.
mikes02
Courses Plus Student 16,968 PointsIt's because you have parenthesis wrapped around your array elements in the loop, you are treating it as if questions was a function and the array elements are its parameters which is not the case, this is causing an error which is preventing your code from executing properly. It should be:
question = questions[i][0];
answer = questions[i][1];