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 trial

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

Chenghua Yang
Chenghua Yang
2,591 Points

Got infinite loop

I got infinite loop error for this challenge. Thought I'd checked 20mins, I still can't find the bug. Please help me with this code on JSBIN ( http://jsbin.com/bequra/edit?js,console,output )

1 Answer

Gunhoo Yoon
Gunhoo Yoon
5,027 Points

You have several name errors that makes code not executable.

First thing first you don't have an infinite loop. What you have is just a warning so you don't have to worry too much. JS Bin kindly tell you to include //noprotect in your code. You can put it anywhere in your code. This is probably JS Bin thing where you don't have to worry about in your actual application, why not just use workspace for treehouse project?

Anyway, actual problems lie across your code.

First, you have naming errors 'resilt' should be changed to 'result'. 'getElementByID' should be changed to 'getElementById'

Second, your 'correct' will result to NaN because you are trying to add number to None value. Fix 'correct' to have initial value of 0 or whatever number you wish to.

Third, loop variable for 2nd and 3rd doesn't have a var keyword. It doesn't have impact for this situation but it is generally considered risky to declare variable without var keyword since they hoist your variable to its local scope.

Chenghua Yang
Chenghua Yang
2,591 Points

Thank you very much! I didn't notice so many errors. After viewing your response, I'd updated a new bin[ http://jsbin.com/fesuwu/edit?js,console,output ]. But I didn't quite know the third point. Why the second and third round did not show up? Also, I made question, answer and response local inside for loop, is this correct? Thanks again. :)

Gunhoo Yoon
Gunhoo Yoon
5,027 Points

Please read what I've wrote put //noprotect in your code as a separate line. Also, your code still don't run because you haven't set the actual HTML id.

Chenghua Yang
Chenghua Yang
2,591 Points

Thanks. I just put //noprotect to make it correct! :D But why there's an undefined before the whole result?

Gunhoo Yoon
Gunhoo Yoon
5,027 Points

Because your 'result' variable's initial value is undefined and you are concatenating string to it.

Chenghua Yang
Chenghua Yang
2,591 Points

Thanks!! So it's better set the initial value of a variable like "", 0 or [] if we'd insert something later on?

Gunhoo Yoon
Gunhoo Yoon
5,027 Points

It entirely depends on what you are going to do with your variable.

Chenghua Yang
Chenghua Yang
2,591 Points

Sure to set something depending on situation. But is it still better to set nothing (like var response;)?

Gunhoo Yoon
Gunhoo Yoon
5,027 Points

It's a stylistic choice you should figure that out.