bhoot4242
bhoot4242

Reputation: 281

Check Id element javascript

var jewels = new Array()
var index
var rand

var scard = [a1, a2, a3, b1, b2, b3, c1, c2, c3]

    function generateBoard() {

        for (var i = 0; i <= 8; i++) {
            jewels[i] = new Array();

            var index = Math.floor(Math.random() * scard.length)
            jewels[i] = scard[index]
            console.log("I:" + i + jewels[i])
            var yy = scard.indexOf(scard[index]);
            scard.splice(yy, 1);
        }
    }

    function drawBoard() {

        generateBoard()
        var html = '<div class=Game_Background>'

        for (var i = 0; i < 9; i++) {

            rand = Math.floor(Math.random() * jewels.length)
            html += '<div class="' + jewels[rand] + '" id="ship' + i + '" style="top:' + ((i * 12) + 135) + 'px; left:245px;" ></div>'
            jewels.splice(rand, 1);
        }
        var newarray = [a1, a2, a3, b1, b2, b3, c1, c2, c3]
    }

here a1,a2,a3,b1,b2,b3,c1,c2,c3 are css class.I use the newarray because I splice all the array items.Now Suppose the element of ship2 id is b3 and b3 is positioned in newarray[5]. How I check this(the id element is positioned in which number of index in the array)?

Upvotes: 1

Views: 242

Answers (1)

Anoop Joshi P
Anoop Joshi P

Reputation: 25527

<script type="text/javascript">
var jewels = new Array()
var index
var rand

var scard = ["a1", "a2", "a3", "b1", "b2", "b3", "c1", "c2", "c3"];
var newarray = ["a1", "a2", "a3", "b1", "b2", "b3", "c1", "c2", "c3"];

drawBoard();



function drawBoard() {


    var html = '<div class=Game_Background>'

    for (var i = 0; i < 9; i++) {

        rand = Math.floor(Math.random() * scard.length);
        html += '<div class="' + scard[rand] + '" id="ship' + i + '" style="top:' + ((i * 12) + 135) + 'px; left:245px;" ></div>'
        alert(newarray.indexOf(scard[rand]));
        scard.splice(rand, 1);
    }
}

</script>

Upvotes: 1

Related Questions