/    Sign up×
Community /Pin to ProfileBookmark

Count number of rolls till doubles

I am fairly new to javascript and am trying to get the following program to work. I need to have the program run until doubles are reached and then display the number of attempts it took ’till the doubles was reached.

<!doctype html>
<!– roll.html Dave Reed –>
<!– This page counts dice rolls until doubles are obtained. –>
<!– ========================================================== –>

<html>
<head>
<title> Dice Roller </title>
<script type=”text/javascript” src=”http://balance3e.com/random.js“>
</script>
<script type=”text/javascript”>
function RollUntilDoubles()
// Assumes: outputDiv is available for output
// Results: rolls and displays dice until doubles are obtained
{
var roll1, roll2;
var doubleCount = 0;

while (roll1 != roll2; doubleCount++) { // WHILE NOT DOUBLES,
roll1 = RandomInt(1, 6); // ROLL AGAIN AND DISPLAY AT
roll2 = RandomInt(1, 6); // THE END OF THE PAGE DIVISION

if (roll1 != roll2) {
doubleCount = doublecount + 1;
alert(‘Number of Rolls: ‘ + doubleCount);
}
document.getElementById(‘outputDiv’).innerHTML =
document.getElementById(‘outputDiv’).innerHTML+
roll1+’-‘+roll2+'<br>’;
}
document.getElementById(‘outputDiv’).innerHTML =
document.getElementById(‘outputDiv’).innerHTML+’DOUBLES!’;

}

</script>
</head>

<body>
<h2>Dice Roller</h2>
<input type=”button” value=”Roll until doubles”
onclick=”RollUntilDoubles();”>
<hr>

<div id=”outputDiv”></div>
</body>
</html>

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@JMRKERMar 19.2012 — Several problems:

1. Syntax error:
<i>
</i>while (roll1 != roll2; doubleCount++) { // WHILE NOT DOUBLES,


  • 2. Two different variables:
    <i>
    </i>doubleCount = doublecount + 1;


  • 3. Missing external file (I assume contains missing RandomInt function)
    <i>
    </i>&lt;script type="text/javascript" src="http://balance3e.com/random.js"&gt;


  • You should enclose your script between [ code] and [ /code] tags (without the spaces)

    to make it easier to read, copy, test and comment on your problems.
    ×

    Success!

    Help @volleyb999 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.19,
    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: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

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