Problem returning text

Asked

Viewed 23 times

0

the console returns the variable as Undefined

carta = new Image();
    carta.src = "images/CartaVirada.png";
    carta.src2 = defineCarta(1);
    carta.position = {x:5, y:10};
    carta.numImg = criarAleatorioUnico();

function defineCarta(numCarta)
{
    if(numCarta == 1)
    {
        if (carta.numImg == 1 | carta.numImg == 2 ) return "images/Carta1.png";
        else
        {
            if (carta.numImg == 3 | carta.numImg == 4 ) return "images/Carta2.png";
                else
                {
                    if (carta.numImg == 5 | carta.numImg == 6 ) return "images/Carta3.png";
                        else
                        {
                            if (carta.numImg == 7 | carta.numImg == 8 ) return "images/Carta4.png";
                        }
                }    
        }

1 answer

1


You’re running defineCarta(1); before carta.numImg = criarAleatorioUnico();, this makes within this function the value carta.numImg does not yet exist.

Change the order of these lines and it will already go as you expect:

the console returns the variable as Undefined

const carta = new Image();
carta.src = "images/CartaVirada.png";
carta.numImg = criarAleatorioUnico();
carta.src2 = defineCarta(1);
carta.position = {x:5, y:10};
  • 1

    That’s right, thank you very much.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.