IberoMedia
IberoMedia

Reputation: 2304

proper javascript for a while loop

I would like to know proper code to add another condition to the while loop code below. Specifically where .id !='k2-label' I would like .id (!= 'k2-label' && !=some-label) (this is the PHP version of what I want to accomplish), except I don't know javascript. The change is to be added to the following code:

while (next.getFirst().getNext().getFirst().id != 'k2-label') {
                        joomla_bits['titles'].push(next.getFirst().getFirst());
                        joomla_bits['items'].push(next.getFirst().getNext().getChildren());
                        next = next.getNext();
                    }

Thank you,

Upvotes: 0

Views: 298

Answers (1)

Jacob
Jacob

Reputation: 78880

You could just say next.getFirst().getNext().getFirst().id != 'k2-label' && next.getFirst().getNext().getFirst().id != 'some-label', but that causes the getFirst().getNext().getFirst().id to be executed multiple times. You should add an intermediate variable:

var id = next.getFirst().getNext().getFirst().id;
while (id != 'k2-label' && id != 'some-label') {
    joomla_bits['titles'].push(next.getFirst().getFirst());
    joomla_bits['items'].push(next.getFirst().getNext().getChildren());
    next = next.getNext();
    id = next.getFirst().getNext().getFirst().id;
}

Upvotes: 2

Related Questions