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 The Solution

Russell Smitheram
Russell Smitheram
2,009 Points

My code keeps hanging...

I've been sailing along fairly well with Javascrpt up until this point.

I'm feeling really frustrated because I can't figure the problem out. The console keeps hanging and I don't understand why.

Can anyone help please?

Here's a snapshot.

https://w.trhou.se/byazztt9kh

I've only got as far as the second loop because I'm testing each loop as I go.

Cheers

1 Answer

Emmanuel C
Emmanuel C
10,636 Points

Hey

In your 2nd for loop it seems that the 'i' variable never increases since its only adding 1 to it but it never reassigned it to 'i'. So 'i' simply stays as 1 and loops infinitely.

Changing

 for ( let i = 1; i <= 5; i + 1 ) 

To

 for ( let i = 1; i <= 5; i++  )  OR  for ( let i = 1; i <= 5; i += 1 ) 

Should fix this, as both statements adds one to 'i' and then reassigns it back into the 'i' during each iteration. That way 'i' can reach 5 and break out of the loop.

Hope that helps