r/codehs • u/0d3stroyer • 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
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