0

I was testing out javascript games and functions and on this particular example it keeps getting stuck after clicking on the play button, after I inspected the elements of the source code it showed me that error is in the two functions pasted below, however I dont know why are these exceptions given , any help would be greatly appreciated

    function rollDice()
        {
            dicerolling.play();

            die1Value = NaN;
            die2Value = NaN;
            showDice();

            die1Value = Math.floor(1 + Math.random() * 6);
            die2Value = Math.floor(1 + Math.random() * 6);
            return die1Value + die2Value;
        }

        function showDice()
        {
            setImage( rollDie1Img, die1Value );
            setImage( rollDie2Img, die2Value );
        }

        function setImage( dieImg, dieValue )
        {
            if ( isFinite ( dieValue ) )
                dieImg.src = "images//" + dieValue + ".png";
            else
                dieImg.src = "images//blank.png";
        }

the complete code is pasted here if needed :

http://codepaste.net/o29v92

the error given by chrome :

chrome error

1 Answer 1

1

You're using the same ID twice in your elements:

<img id="rollDie1" src = "images/blank.png" alt = "Die 1 of Roll Value">
<img id="rollDie1" src = "images/blank.png" alt = "Die 1 of Roll Value">

Judging from your code, you should just use rollDie2 for the second one:

<img id="rollDie1" src="images/blank.png" alt="Die 1 of Roll Value">
<img id="rollDie2" src="images/blank.png" alt="Die 2 of Roll Value">

jsFiddle here

Sign up to request clarification or add additional context in comments.

2 Comments

oh wow I spent so much time but only at the script section , should pay more attention on the body part , thank you so much for pointing it out, but I must admit these things sure do make me confused sometimes
@Rage91 No worries, I used console.log() to track what was dieImg was and noticed that it was working for the first time but not the second, that's why I went to the HTML

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.