2 Replies - 1352 Views - Last Post: 05 April 2012 - 04:22 PM

#1 soccerjam2  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 05-April 12

Rock Paper Scissors in Javascript

Posted 05 April 2012 - 12:47 PM

I am trying to create a simple Javascript game of Rock Paper Scissors and I just need help with one quick thing. I need to make it so that when the user clicks the image of either rock paper or scissors, the image of the hand (a different image) changes to mimic what they clicked. Below is the code for just changing the image, for some reason the image will not show up. Please help. Thanks!

<table>
        <tr>
            <td colspan="3">Rock Paper Scissors</td>
        </tr>
        <tr>
            <td onclick="playGame('rock');"><img src="rock.jpg" alt="rock" /></td>
            <td rowspan="3"><img id="leftHand" src="leftPaperHand.jpg" alt="leftHandPaper" /></td>
            <td rowspan="3"><img id="rightHand" src="rightRockHand.jpg" alt="rightHandRock"/></td>
        </tr>
        <tr>
            <td onclick="playGame('paper');"><img src="paper.jpg" alt="paper" /></td>
        </tr>
        <tr>
            <td onclick="playGame('scissors');"><img src="scissors.jpg" alt="scissors" /></td>
        </tr>
        <tr>
            <td colspan="3"><p>Wins: Losses: Ties:</p></td>
        </tr>
    </table>
    
    <script type="text/javascript">
        picForLeftPlayer = "leftPaperHand.jpg";
        picForRightPlayer = "rightRockHand.jpg";
        function playGame (choice){
            switch (choice){
                case 'rock':
                    document.getElementById("leftHand").src="leftRockHand.jpg";
                case 'paper':
                    document.getElementById("leftHand").src="leftPaperHand.jpg";
                case 'scissors':
                    document.getElementById("leftHand").src="leftScissorsHand.jpg";
            }
        }
    </script>



Is This A Good Question/Topic? 0
  • +

Replies To: Rock Paper Scissors in Javascript

#2 e_i_pi  Icon User is offline

  • = -1
  • member icon

Reputation: 745
  • View blog
  • Posts: 1,521
  • Joined: 30-January 09

Re: Rock Paper Scissors in Javascript

Posted 05 April 2012 - 04:16 PM

You're missing a break after each case statement:
<table>
        <tr>
            <td colspan="3">Rock Paper Scissors</td>
        </tr>
        <tr>
            <td onclick="playGame('rock');"><img src="rock.jpg" alt="rock" /></td>
            <td rowspan="3"><img id="leftHand" src="leftPaperHand.jpg" alt="leftHandPaper" /></td>
            <td rowspan="3"><img id="rightHand" src="rightRockHand.jpg" alt="rightHandRock"/></td>
        </tr>
        <tr>
            <td onclick="playGame('paper');"><img src="paper.jpg" alt="paper" /></td>
        </tr>
        <tr>
            <td onclick="playGame('scissors');"><img src="scissors.jpg" alt="scissors" /></td>
        </tr>
        <tr>
            <td colspan="3"><p>Wins: Losses: Ties:</p></td>
        </tr>
    </table>
    
    <script type="text/javascript">
        picForLeftPlayer = "leftPaperHand.jpg";
        picForRightPlayer = "rightRockHand.jpg";
        function playGame (choice){
            switch (choice){
                case 'rock':
                    document.getElementById("leftHand").src="leftRockHand.jpg";
                    break;
                case 'paper':
                    document.getElementById("leftHand").src="leftPaperHand.jpg";
                    break;
                case 'scissors':
                    document.getElementById("leftHand").src="leftScissorsHand.jpg";
                    break;
            }
        }
    </script>


Was This Post Helpful? 0
  • +
  • -

#3 soccerjam2  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 05-April 12

Re: Rock Paper Scissors in Javascript

Posted 05 April 2012 - 04:22 PM

THANK YOU!!!!!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1