Search code examples
javascriptarrayschess

JS returns only 3 elements from 32 element array


I am trying to get all the chess pieces present on the board. Array seems to be successfully filled up with 32 pieces(Which is valid for the starting position). But, in switch comparison, it finds only 3 of them. I am using the img src parameter, to distinguish the pieces. JS code for that:

function SetupPieces()
{
    var piecesObj = document.getElementsByClassName("chess_com_piece chess_com_draggable");
    let imgSrc = [];
    for(var i=0;i<piecesObj.length;i++)
    {
        imgSrc[i] = piecesObj[i].src.substring(60);
    }
    var Pieces = [], FirstPosition = [], SecondPosition = [], BRookX,BRookY, WRookX,WRookY,BRook,WRook;
    for(var i=0;i<imgSrc.length;i++)
    {
        var TempPieceLocObj = (piecesObj[i].style.transform).substring(10);
        TempPieceLocObj = (TempPieceLocObj.substring(0,TempPieceLocObj.length - 1)).split(",");

        FirstPosition[i] = (TempPieceLocObj[0].split("px"))[0];
        SecondPosition[i] = (TempPieceLocObj[1].split("px"))[0];
        console.log("Array size - " + imgSrc.length);
        switch(imgSrc[i])
        {
            case "wp.png":
                Pieces[i] = "White Pawn";
                break;
            case "bp.png":
                Pieces[i] = "Black Pawn";
                break;
            case "br.png":
                Pieces[i] = "Black Rook";
                BRook = (piecesObj[i].style.transform).substring(10);
                BRook = (BRook.substring(0,BRook.length - 1)).split(",");
                BRookY = parseInt(BRook[1].substring(0,BRook[1].length - 3),10) * 1.029684601113173;
                BRookX = parseInt(BRook[0].substring(0,BRook[1].length - 1),10) * 1.029684601113173;
                break;
            case "wr.png":
                Pieces[i] = "White Rook";
                WRook = (piecesObj[i].style.transform).substring(10);
                WRook = (WRook.substring(0,WRook.length - 1)).split(",");
                WRookY = parseInt(WRook[1].substring(0,WRook[1].length - 1),10) * 1.029684601113173;
                WRookX = parseInt(WRook[0].substring(0,WRook[1].length - 3),10) * 1.029684601113173;
                break;
            case "bn.png":
                Pieces[i] = "Black Knight";
                break;
            case "wn.png":
                Pieces[i] = "White Knight";
                break;
            case "bb.png":
                Pieces[i] = "Black Bishop";
                break;
            case "wb.png":
                Pieces[i] = "White Bishop";
                break;
            case "wq.png":
                Pieces[i] = "White Queen";
                break;
            case "bq.png":
                Pieces[i] = "Black Queen";
                break;
            case "bk.png":
                Pieces[i] = "Black King";
                break;
            case "wk.png":
                Pieces[i] = "White King";
                break;
        }
    }
    if(WRookX !== 0) {BoardSize = WRookX;}
    return [FirstPosition,SecondPosition,Pieces];
}

Console output - https://prnt.sc/uhrg79

Pieces in imgSrc array - https://prnt.sc/uhrgxz

I am outputting them using this simple code:

var testing = SetupPieces();

for(var i = 0; i<testing.length;i++)
{
    console.log("X:" + testing[0][i] + " Y:" + testing[1][i] + " Name:" + testing[2][i]);
}

Solution

  • testing will be equal to the return of the function ([FirstPosition,SecondPosition,Pieces]) and therefore testing.length will be 3. You probably mean testing[2].length, which are the pieces?