Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Feb 2013
    Thanked 0 Times in 0 Posts

    Building an Image into a function

    I'm a complete beginner to javascript and I'm curious what's wrong with my code. We need to make one original image appear, and then upon clicking two images need to appear that both simulate a die throw, so randomly being 1 through 6. Please help!

    <!DOCTYPE html>
    	<title>More Games</title>
    function SelectImages()
    // Assumes: die images are in http://pages.uoregon.edu/alans/111/CIS%20111/images/
    // Results: displays a randomly selected image of a 6-sided die
    roll1 = Math.floor(Math.random() * 6) + 1;
    imgName = '../images/die' + roll1 + '.gif';
    document.getElementById('dieImage').src = imgName;
    var dieImg2 = roll1 = '../images/die' + roll2 + '.gif';
    var myNewTag = "<img id='dieImg2' alt='die Image' src='../images/die1.gif'>"
    document.getElementById('dieImage').innerHTML += myNewTag;
    	<div style="text-align:center">
    		<h2>Doubles Simulation</h2>
    			<img id="dieImage" alt="die image" src="../images/die1.gif">
    		<input type="button" value="Click to Play" onclick="SelectImages();">
    	<a href="http://pages.uoregon.edu/alans/111/CIS%20111/p3/slots.html">In Class Slots Exercise</a>
    	<a href="http://pages.uoregon.edu/alans/111/CIS%20111/p3/InClassDiceStats.html">In Class Dice Counter Exercise</a>
    		We reviewed for the Midterm Exam
    			&copy; Copyright  by Alan Sylvestre
    			<a href="mailto:alans@uoregon.edu">Email Me</a>
    			<a href="http://cargocollective.com/alansylvestre">View My Website Here</a>

  2. #2
    Master Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Behind the Wall
    Thanked 567 Times in 560 Posts
    you don’t roll for the second die.

    and since the image (dieImage) is an <img>, it doesn’t have content you could address with innerHTML.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer


Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts