/    Sign up×
Community /Pin to ProfileBookmark

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!

[CODE]
<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”>
</form>
<div id= “outputavh”></div>
<div id= “outputavt”></div>
</div>

[/CODE]

[CODE]
<script>

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()
{
setImage(coinImage);
updateTable();

}
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;
}
else
{
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);
</script>
[/CODE]

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@nap0leonApr 25.2012 — 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.
×

Success!

Help @Datastream spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.4,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...