Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript Loops, Arrays and Objects Tracking Multiple Items with Arrays Build a Quiz Challenge, Part 1 Solution

Uche Onuekwusi
Uche Onuekwusi
16,595 Points

The second question keeps looping pls help me out

var questions = [ ["Who is the president of Nigeria ?", "buhari"], ["Dublin is the capital of which country ?", "ireland"], ["Manchester united football club is from which country ?", "england"]

]; var correctanswer= 0; var question; var response; var answer; var html;

for (var i = 0; i < questions.length; i=+1) { question = questions[i][0]; answer = questions[i][1]; response= prompt(question); response = response.toLowerCase(); if (response === answer ) { correctanswer=+1;

}   


}

html = "you got " + correctanswer + " question(s) right" ;

document.write(html);

I ran your code and it works fine. Can you explain more of what happens? You can also format the code to make it more presentable?

Steven Parker
Steven Parker
212,135 Points

The formatting ammarkhan is suggesting can be done by using the instructions in the Markdown Cheatsheet pop-up below the "Add an Answer" area. :arrow_heading_down:   Or watch this video on code formatting.

1 Answer

Steven Parker
Steven Parker
212,135 Points

The increment clause in your "for" loop is "i = +1". This keeps setting the index to 1 (+1), which refers to the second question.

You probably intended to use the addition assignment operator to increment the value instead: "i += 1".