Yranna
Yranna

Reputation: 31

if img.src === img.src === img.src - cant get it to work

I am trying to make a Match3 like Game with images, but I cant get the comparison to work. I am doing this for a fixed number atm, just for getting it to work, should be in a foreach loop later. The function is inside a "DOMContentLoaded" Listener if that makes any difference. This: if statement with img.src and this: Getting img.src value (path name) did not help me and I am not able to find or see a solution. Thanks for any help or suggestions!

function removeTiles() {
    tiles = document.getElementsByTagName("IMG");
    /*for ( var j = 0; j < tiles.length; ++j) {*/
        var x = 15;
        var y = x-1;
        var z = x+1;

       /*I tried it with tiles[x].src before, gave me the same console and alert output, but the other post suggested trying this, so I did */
        var x2 = tiles[x].getAttribute('src', 2);
        var y2 = tiles[y].getAttribute('src', 2);
        var z2 = tiles[z].getAttribute('src', 2);
        alert(x2);
        alert(y2);
        alert(z2);


        if (x2 === y2 === z2){
            tiles[z].parentNode.removeChild(tiles[z]);
            tiles[y].parentNode.removeChild(tiles[y]);
            tiles[x].parentNode.removeChild(tiles[x]);

        } else {

            console.log('whatever');
            console.log(x2);
            console.log(y2);
            console.log(z2);
        }

        console.log(tiles[x]);
        }

    /*}*/
window.onload = removeTiles();

The console output is:

whatever    
 coffee.gif
 coffee.gif
 coffee.gif
<img src="coffee.gif">   

Upvotes: 0

Views: 79

Answers (1)

Korgen
Korgen

Reputation: 5399

you cannot chain them like x2 === y2 === z2 the first part will evaluate to a boolean (i.e. true or false) which compared via === to a string will always yield false.

You will need to do it like x2 === y2 && y2 === z2

Upvotes: 5

Related Questions