jon
jon

Reputation: 1808

loop with modulus % JS

Is there a more readable or correct way through a loop to build a table with up to 5 elements before the new line. I use the modulus operator, but I'm sure it has a more readable or optimized way.

So I need to make a loop, and every time "i" can divide by 5 I jump on a new line. Do you have more suggestions or my code is perfect?

function refreshLibs() {
    let list = sheet_selected==="all" && $PME.libraryObj || _sortSheetType($PME.libraryObj);
    list = cat_selected==="all" && list || _sortCategory(list);

    for (let [i,len,x,y,m,l] = [0,list.length,0,0,50,1]; i < len; i++) { // m:marge l:line
        const cage = list[i];
        [cage.x,cage.y] = [x,y];
        if( !((i+1)%6) ){ [x,y,l] = [0,y+cage.height+20,++l] } // this can be optimize ???
        else{x+=cage.width+m;};
        cage_library.addChild(cage);
    };
};

Upvotes: 1

Views: 1255

Answers (1)

jon
jon

Reputation: 1808

wow just find a math experimentation just give me the magic code for jump loop line!

    for (let i=x=y=l= 0, len = list.length; i < len; i++) {
        const cage = new PIXI.Container();
        const cage_Bg = drawRec(0, 0, 170, 122);
        cage_Bg.x = x+150, cage_Bg.y = y;
        cage.addChild(cage_Bg);
        Cage_Libs.addChild(cage);
        x+=cage_Bg.width+marX;

        if(!(~i%~4)){ // MAGICCCCCS JUMP EACH 4*(I) and loop and loop allow start at 0
         x=0,y+=marX;
        }
    };

Upvotes: 1

Related Questions