user4544229
user4544229

Reputation:

Javascript code for newbies

Im a newbie in javascript and i want you to help me to transform this code from for to while

var el = document.getElementsByClassName('_3hqu'); 
for(var i=0; i<el.length;i++) { 
el[i].click(); 
}

When i run this code in chrome console it works. But when i try my own while code its not working, can you help me find my errors?

    while (true)
{
    if ( el[0].length != 1 )
    {
        if ( el[1].length = 0 )
        {
            el[1].click();
        }
        else
        {
            break;
        }
    }
    else if ( el[0].length = 0 )
    {
        el[0].click();
    }
    else
    {
        break;
    }
var el = document.getElementsByClassName('_3hqu'); 
}

Also if i want to check all the "el" and when it finds the last one to scroll down???

check this out

var el = document.getElementsByClassName('_3hqu'); 
    var objDiv = document.querySelector("._1v31 .uiScrollableAreaWrap");
    var n = -1;
    while (true)
    {
        if (n == el.length) 
        {
            break;
        }   
        else
        {
            objDiv.scrollTop = objDiv.scrollHeight;
        }
         n = el.length;
         var el = document.getElementsByClassName('_3hqu'); 
    }

Something like this one, but this is not working. i get the error

TypeError: Cannot read property 'scrollHeight' of null

Thank you

Upvotes: -1

Views: 77

Answers (5)

das-g
das-g

Reputation: 9994

while (true)
{
    if ( el[0].length != 1 )
    {
        if ( el[1].length = 0 ) // should probably be a comparison (==), not an assignemnt (=)
        {
            el[1].click();
        }
        else
        {
            break;
        }
    }
    else if ( el[0].length = 0 ) // should probably be a comparison (==), not an assignemnt (=). If so, will this condition every change during the loop, so that it will actually terminate at some time?
    {
        el[0].click();
    }
    else
    {
        break;
    }
    var el = document.getElementsByClassName('_3hqu'); // This should probably be done before the loop, rather than in it. Otherwise, what will `el` be before this assignment?
}

Upvotes: 0

Petr Jindra
Petr Jindra

Reputation: 49

I do not know why you want to use this ugly while instead of for but it is your code. Main problem with this code is in condition:

length = 0

with this you assign into length attribute value 0. To compare you have to use:

length == 0

Upvotes: 0

Guffa
Guffa

Reputation: 700840

A for loop can be transformed to a while loop like this:

for (a; b; c) {
  d;
}

to:

a;
while (b) {
  d;
  c;
}

So your for loop as a while would just be:

var el = document.getElementsByClassName('_3hqu'); 
var i = 0;
while (i < el.length) {
  el[i].click();
  i++;
}

Upvotes: 3

Keith Nicholas
Keith Nicholas

Reputation: 44316

not quite sure what you are trying to do, but to convert it to the equivalent while loop

var i=0;
while(i<el.length) { 
  el[i].click(); 
  i++;
}

Upvotes: 1

Fruitdrops13
Fruitdrops13

Reputation: 21

First there is something wrong in your if blocks: el[1].length = 0 you are assigning, not value checking.

Change that to: el[1].length == 0

The same for the other line: el[0].length = 0 to el[0].length == 0

Upvotes: 0

Related Questions