Results 1 to 2 of 2

Thread: Help with Buttons!

  1. #1
    Join Date
    Apr 2012

    Help with Buttons!

    I am very new with Javascript and implementing it on a webpage but trying to learn.

    I'm trying too make a simple set of functions that will flip a coin and record the resulting frequencies of heads vs tails. I also want the image to switch from heads/tails depending on how the flip function lands the coin.

    I've been working on this for three days straight with no progress.

    Please help!

    <script src= "../hw4/hw4.js"></script>
    <div class="content">
    	<img id = "coinImage1" src = "../images/coinh.jpg" alt = "Coin Image">
      <form action = "#">
    	<input id = "toss" type = "button" value = "Toss">
    	<div id= "outputavh"></div>
    	<div id= "outputavt"></div>
    var freqh = 0;
    var freqt = 0;
    var avh = 0;
    var avt = 0;
    var coinImage;
    function start()
    	var button = document.getElementById("toss");
    	button.addEventListener("click", flipCoin(), false);
    	coinImage = document.getElementById("coinImage1");
    function flipCoin()
    function setImage(sideImage)
    	var coinValue = Math.floor(1 + Math.random() * 2);
    	if(coinValue == 1)
    		sideImage.setAttribute("src", "../images/coinh.jpg");
    		sideImage.setAttribute("alt", "Coin facing Heads");
    		freqh += 1;
    		sideImage.setAttribute("src", "../images/coint.jpg");
    		sideImage.setAttribute("alt", "Coin facing Tails");
    		freqt += 1;
    function updateTable()
    	avh = <h1>(freqh / (freqh + freqt))</h1>;
    	avt = <h2>(freqt / (freqh + freqt))</h2>;
    	var avhOut = document.getElementById("outputavh");
    	var avtOut = document.getElementById("outputavt");
    	avhOut.innerHTML = avh;
    	avtOut.innerHTML = avt;
    window.addEventListener("load", start, false);

  2. #2
    Join Date
    Nov 2007
    A couple changes:
    1- for your output, you need to put your <h1> tags in quotes.
    2- your math.random was missing parens around Math.random*2
    3- I removed you event listener and placed the call directly onto the image. You can probably add this back, but I removed it so that it was one less thing to trouble shoot.
    4- your "start" function is not required
    5- "coinimage" was declared inside a function and had no scope where it was used later.

    There may be a couple other minor changes that I've forgotten.

    Since this is obviously a homework assignment, I will not post the working code. But the above should be enough to point out your errors.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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