Matt Biggs
Matt Biggs

Reputation: 177

Javascript and for loops not working

I am beyond stumped on this one..

Here is the code I am executing.

   var Category = [];

    //START OF VARIABLES

    var ID1;
    var ID2;
    var ID3;
    var ID4;
    var ID5;

    var temp1;
    var temp2;
    var temp3;
    var temp4;
    var temp5;

    var extracats2;

    var Level2CatsLen;
    var Level3CatsLen;
    var Level4CatsLen;
    var Level5CatsLen;

    var Level2Children = [];
    var Level3Children = [];
    var Level4Children = [];
    var Level5Children = [];

    //END OF VARIABLES

        var dispatchMouseEvent = function(target, var_args) {
                var e = document.createEvent("MouseEvents");
        e.initEvent.apply(e, Array.prototype.slice.call(arguments, 1));
        target.dispatchEvent(e);
                target.addEventListener("click",Level2CatsLen = GetLevel2, true);

    }

    var Level1Cats = document.getElementsByClassName("p-pstctgry-lnk-ctgry "); //GETTING LEVEL 1 CATS

    var Level1CatsLen = Level1Cats.length; //GETTING LEVEL 1 CAT LEN

        function GoToLevel2(n) { //GO TO NEXT LEVEL!
        setTimeout(function() {
        ID1 = Level1Cats[n].id;
        temp1 = Level1Cats[n].innerHTML;
        temp1 = temp1.replace(/&amp;/gi, "&").replace(/<[^>]*>/gi, "").replace(/<[^>]*>/gi, "");
        console.log(temp1);
        dispatchMouseEvent(Level1Cats[n], "mouseover", true, true);
        dispatchMouseEvent(Level1Cats[n], "click", true, true);
        }, i*2000);
        }

        function GoToLevel3(n) { //GO TO NEXT LEVEL!
        setTimeout(function() {
        ID2 = Level1Cats[n].id;
        temp2 = Level1Cats[n].innerHTML;
        temp2 =temp2.replace(/&amp;/gi, "&").replace(/<[^>]*>/gi, "").replace(/<[^>]*>/gi, "");
        console.log(temp2);
        dispatchMouseEvent(Level3Cats[n], "mouseover", true, true);
        dispatchMouseEvent(Level3Cats[n], "click", true, true);
        }, i*1500);
        }

        function GetLevel2() { 
        setTimeout(function() {
            Level2Children = [];
        Level2Cats = document.getElementsByClassName("p-pstctgry-lnk-ctgry");
        console.log("Level1CatLen: "+Level1CatsLen);
        var Level2CatsLen = Level2Cats.length
        console.log("Level2CatLen: "+Level2CatsLen);

        extracats2 = Level2CatsLen - Level1CatsLen;
        console.log("EXTRACATS2: "+extracats2);


            console.log("TRYING CAT 2");

            //#### LEVEL 3 ####
            for (ii = 0; ii < extracats2; ii++) {
            if (extracats2 < 2) {
            alert("lel");
                GoToLevel3(ii);     

            Level2Children.push(Level2Cats[ii+Levael1CatsLen].id);
            }

        //DO SOMETHING WITH CATEGORIES - Level 1

        Category.push({Name: temp1, ID: ID1, ParentID: 'null', ChildrenIDs: Level2Children});
        console.log("LELLELLEL");
        //FINISH


        return Level2CatsLen;
        }},3000);
        }

    //THE ACTUAL METHOD!

        for (i = 0; i <= Level1CatsLen-1; i++) {
        //#### LEVEL 2 ####
        GoToLevel2(i);


        // END OF LEVEL 1
    }

It kind of runs as intended..

So it gets to the following for loop but for some reason doesn't meet the condition.

//#### LEVEL 3 ####
for (ii = 0; ii < extracats2; ii++) {
if (extracats2 < 2) {
alert("lel");
    GoToLevel3(ii);     

Level2Children.push(Level2Cats[ii+Levael1CatsLen].id);
}

As you can see I have an alert in there but it never actually does it.

I get an output above of extracats that says all of them except one category are above 2 and hence should execute the following code within the for loop. Though it does..

Any ideas?

Upvotes: 0

Views: 77

Answers (2)

Sean Missingham
Sean Missingham

Reputation: 938

Razroo made some very good points, and I fully agree with him.

It looks like you've said you want numbers "above 2", and yet your if statement is looking for those that are less than two.

I think you need to change it up to be:if (extracats2 > 2) {

Simple mistake, easily overlooked :)

Upvotes: 1

Charlie-Greenman
Charlie-Greenman

Reputation: 1659

Hey just a couple of suggestions, not sure if this is it.

  1. Not sure if it is your code, or others. Regardless, a semicolon on line 37 is a good idea.
  2. var instead of implicit declaration(i.e. no var) is always a good idea.
  3. With regards to the for loop , it does not loop. You are returning Level2CatsLen before it finishes, which is not even a part of the for loop, so the value will stay the same.
  4. var Level2CarsLen was never terminated with a semicolon on line 70, so that could also be a part of the issue
  5. Also, Levael1Cats1Len is spelled wrong. It should be Level1CatsLen instead, without the a.
  6. Also the function GetLevel2(){ that starts at line 65 ends on line 97. So maybe you want it to finish before then.

Hopefully one of those helps. You are doing great, keep at it, even if one of these do not do it!

Upvotes: 2

Related Questions