/    Sign up×
Community /Pin to ProfileBookmark

Roll counter Problem

Hi I’ve made a code that rolls dice untill it gets doubles, I’m trying to make a Counter for it so that it counts how many times it rolls untill it gets doubles. Can someone help me get the counter to work?

[CODE]<html>
<!– roll.html –>
<!—————————————————————->

<head>
<title> Dice Stats </title>
<script type=”text/javascript”
src=”http://www.prenhall.com/reed/random.js”>
</script>
<script type=”text/javascript”>
function RollUntilDoubles()
// Assumes: document.DiceForm.Output is available for output
// Results: rolls and displays dice until doubles are obtained
{
var roll1, roll2, repCount;

repCount = 0;
while (repCount > 0) {
roll1 = RandomInt(1, 6); // roll and display dice
roll2 = RandomInt(1, 6);
document.DiceForm.Output.value =
“You rolled: ” + roll1 + ” ” + roll2 + “n”;

while (roll1 != roll2) { // while not doubles,
roll1 = RandomInt(1, 6); // roll again and display
roll2 = RandomInt(1, 6);
document.DiceForm.Output.value =
document.DiceForm.Output.value +
“You rolled: ” + roll1 + ” ” + roll2 + “n”;
}
document.DiceForm.Output.value =
document.DiceForm.Output.value + “DOUBLES!”;
document.DiceForm.count.value =
document.DiceForm.count.value + repCount;
}
</script>

</head>

<body>
<div style=”text-align:center”>
<form name=”DiceForm”>
<input type=”button” value=”Roll until doubles”
onClick=”RollUntilDoubles();” />
<br /><br />
<textarea name=Output rows=20 cols=20 wrap=”virtual”></textarea>
<input type=”text” name=”count” size=6 value=0 />
</form>
</div>

</body>
</html>[/CODE]

to post a comment
JavaScript

14 Comments(s)

Copy linkTweet thisAlerts:
@JMRKERDec 14.2007 — I must be caught in homework hell.

See: http://www.webdeveloper.com/forum/showthread.php?t=168450

Modify to your hearts content.
Copy linkTweet thisAlerts:
@heyushooshauthorDec 14.2007 — Can you help me out jr?

I understand what you did with the numbers which helps me out ?

But I need help implementing a counter.
Copy linkTweet thisAlerts:
@JMRKERDec 14.2007 — In the posted code referenced in post #2, change to:
[code=php]
while (PickNo < 1) {
roll1 = Math.floor(Math.random()*6+1);
roll2 = Math.floor(Math.random()*6+1);
if (roll1 == roll2) { document.getElementById('Pick'+PickNo).value = roll1; PickNo++; }
repCount = repCount + 1;

// alert(repCount+' : '+roll1+' : '+roll2); // for testing only
}
[/code]

Remove the alert to display the number of times ONE PICK needs to be rolled

for doubles to occur. ?
Copy linkTweet thisAlerts:
@heyushooshauthorDec 14.2007 — I'm trying to implement that code into:

[CODE]html>
<!-- roll.html 13.3* Jake COoley -->
<!---------------------------------------------------------------->

<head>
<title> Dice Stats </title>
<script type="text/javascript"
src="http://www.prenhall.com/reed/random.js">
</script>
<script type="text/javascript">
function RollUntilDoubles()

{
var roll1 = 0;
var roll2 = 0;
var repCount = 0;
var PickNo = 0;

repCount = 0;
while (repCount > roll1 && roll2) {
roll1 = Math.floor(Math.random()*6+1); // roll and display dice
roll2 = Math.floor(Math.random()*6+1);
document.DiceForm.Output.value =
"You rolled: " + roll1 + " " + roll2 + "n";

while (roll1 != roll2) { // while not doubles,
roll1 = RandomInt(1, 6); // roll again and display
roll2 = RandomInt(1, 6);
document.DiceForm.Output.value =
document.DiceForm.Output.value +
"You rolled: " + roll1 + " " + roll2 + "n";
}
document.DiceForm.Output.value =
document.DiceForm.Output.value + "DOUBLES!";
document.DiceForm.count.value =
document.DiceForm.count.value + repCount;
}
</script>
</head>
<body>
<div style="text-align:center">
<form name="DiceForm">
<input type="button" value="Roll until doubles"
onClick="RollUntilDoubles();" />
<br /><br />
<textarea name=Output rows=20 cols=20 wrap="virtual"></textarea>
<input type="text" name="count" size=6 value=0 />
</form>
</div>
</body>
</html>[/CODE]


Can you help me a bit more? ?
Copy linkTweet thisAlerts:
@JMRKERDec 14.2007 — Well for starters,
[code=php]
while (repCount > roll1 && roll2) {
[/code]

won't work.

You are testing an counter to be greater than a boolean result.

Boolean false is equivalent to '0' so the first pass through the loop

will bypass all following logic in block.

Scatter a few alert() messages to prove to yourself what's going on at various steps.
Copy linkTweet thisAlerts:
@heyushooshauthorDec 14.2007 — What should I replace it with?
Copy linkTweet thisAlerts:
@heyushooshauthorDec 14.2007 — I've tried several different things and I haven't noticed anything different, it's not even rolling doubles...
Copy linkTweet thisAlerts:
@JMRKERDec 14.2007 — It depends upon what you are trying to do.

Your design ... but if you look at the while loop

the current design will never pass even if you change it to (roll1+roll2)

inside the while ( ) because you never increment 'repCount' beyond zero.

I assume the errors you are getting are

1. The loop never executes

or

2. The loop never quits (time out error).

You can try:
[code=php]
while (repCount != (roll1 + roll2)) {
repCount++; // or repCount = repCount + 1;
...
}
[/code]

No guarantees ... I have not run your code with or without any changes.

What does your error console say?

Have you tried putting in some alerts() to see what's going on yet?

How much of the book have you read yet?
Copy linkTweet thisAlerts:
@Arty_EffemDec 14.2007 — Hi I've made a code that rolls dice untill it gets doubles, I'm trying to make a Counter for it so that it counts how many times it rolls untill it gets doubles.[/QUOTE]Had you written that code yourself, you would have no trouble adding the functionality that you want, and you're not fooling anyone by posting the same question under two different names.
Copy linkTweet thisAlerts:
@heyushooshauthorDec 14.2007 — When I load the page it says:

Line:40

Char:4

Error: Expected '}'

Code: 0


When I click the Roll Doubles button I get an error that says:

Line: 46

Char: 1

Error: Object Expected

Code: 0
Copy linkTweet thisAlerts:
@JMRKERDec 14.2007 — Your answer is ... fix the errors.

Your question is ?
Copy linkTweet thisAlerts:
@heyushooshauthorDec 14.2007 — I'm really not sure how to fix these errors... I'm not sure what I am doing wrong.

Can you in anyway help me recode this or make it work?
Copy linkTweet thisAlerts:
@JMRKERDec 14.2007 — Nope ... you have enough hints already!

Getting late here and my bed awaits ....

Good Luck!
Copy linkTweet thisAlerts:
@heyushooshauthorDec 14.2007 — Well.. Thanks for the help with my other problem but There was no point of giving me any help on this one because I've tried all variations of things you tried to help me with...
×

Success!

Help @heyushoosh 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.20,
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,
)...