Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript for loop until - multiple conditions

I am using javascript, using regex to scrape images from html code.

I want the loop to run either until the script finds no more images or until it reaches 12.

I'm trying the following but not working:

var imgs = d.getElementsByTagName('img'), found = [];
for(var i=0,img; ((img = imgs[i]) || ( $i < 13)); i++)

Is this possible? Am I on the right lines?

Quite new to javascript but trying!

like image 835
StudioTime Avatar asked Oct 02 '12 13:10

StudioTime


People also ask

Can you have multiple conditions in a for loop JavaScript?

This is fine for most situations in which a for loop is necessary, but some might not know you can have multiple conditions in a single for loop just like you would in an if statement for example.

Can for loop have multiple conditions?

It do says that only one condition is allowed in a for loop, however you can add multiple conditions in for loop by using logical operators to connect them.

How do you add conditions to a while loop?

Suppose in a while loop, you have two conditions, and any one needs to be true to proceed to the body, then in that case you can use the || operator between those two conditions, and in case you want both to be true, you can use && operator. By using if statements and logical operators such as &&, 11,!

Can we use two conditions in while loop in Java?

It works well with one condition but not two.


1 Answers

You should use && instead of ||. Also, $i should be i.

for(var i=0, img; (img = imgs[i]) && (i < 12); i++)
     found.push(img);
like image 136
I Hate Lazy Avatar answered Sep 28 '22 00:09

I Hate Lazy