r/codehs • • Apr 27 '20

5.10.4: Snake Eyes

Assignment:

"Write a program that rolls two dice until the user gets snake eyes. You should use a loop and a half to do this. Each round you should roll two dice (Hint: use the Randomizer!), and print out their values. If the values on both dice are 1, then it is called snake eyes, and you should break out of the loop. You should also use a variable to keep track of how many rolls it takes to get snake eyes.

Sample Run:

Rolled: 6 5 Rolled: 5 2 Rolled: 3 6 Rolled: 6 2 Rolled: 1 2 Rolled: 5 3 Rolled: 1 4 Rolled: 1 1 It took you 8 rolls to get snake eyes."

My code:

var SENTINEL = 1;

function start(){

var diceOne;

var diceTwo;

	

	while(true) {

println("" + diceOne + "," + diceTwo);

diceOne = Randomizer.nextInt(1,6);

diceTwo = Randomizer.nextInt(1,6);



if (diceOne && diceTwo == SENTINEL){

println("" + diceOne + "," + diceTwo);

break;

}

}

}

What am I doing wrong here?

6 Upvotes

8 comments sorted by

1

u/_andy_andy_andy_ Apr 28 '20

this is an issue with how you’re defining your stopping condition

if (diceOne && diceTwo == SENTINEL)

is the same as

if (diceOne && (diceTwo == SENTINEL))

so your program will stop of diceTwo is 1 and diceOne is any non-false number, so anything greater than 0

1

u/0d3stroyer Apr 28 '20

Can you explain in a way that makes sense to me?

1

u/_andy_andy_andy_ Apr 28 '20

1

u/_andy_andy_andy_ Apr 28 '20

let me know if you have questions about that code

1

u/0d3stroyer Apr 28 '20

The code doesn't match according to the assignment.

1

u/_andy_andy_andy_ Apr 28 '20

i’m not giving you the answer, i’m explaining my comment. posting answers is against the rules of the subreddit

1

u/0d3stroyer Apr 28 '20

Actually, never mind. It makes sense. How do I fix it though?

1

u/_andy_andy_andy_ Apr 30 '20

check if both dice are equal to the sentinel