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

Francesco Paolini
Francesco Paolini
9,910 Points

loop + push + array

I'm trying to push a series of images with id of #img-0, img-1, img-3 etc.. inside the array called imgFolder. I'm trying like this, which I don't know if it's correct cause i'm a beginner, but it seems to crash the browser when i run the html page. it crash the same way it does when it try to run an infinite loop.

let imgFolder = [];

for(i=0; i=4; i+=1){ let imgSelector = document.querySelector('#img-'+i); imgFolder.push(imgSelector); }

3 Answers

Steven Parker
Steven Parker
231,153 Points

You're exactly right, it's an infinite loop because the conditional clause is "i=4" which is always "true".

You probably meant to write "i<4" instead.

Francesco Paolini
Francesco Paolini
9,910 Points

why i=4 is always true if I can ask?

Steven Parker
Steven Parker
231,153 Points

The value of an assignment is the value assigned (4 in this case), and any non-zero value is considered a "true" when used as a boolean.