[Solved]1 Point Using Statement Loop Common Thing Use Variables Keep Track Sort Count Used Loop Co Q37203056
(1 point) Using an if Statement In a Loop
A common thing to do is to use variables to keep track of somesort of count. When used in a loop we count things veryquickly.
Scenario: If you roll a pair of dice, rolling a 12 (two sixes)is rare. How rare? If you were to roll a pair of dice 1,000 times,on average, how many times would it come up as 12?
To figure this out, we could write code to run an experiment. Itwould go something like this:
- Make a loop that simulates rolling a pair of dice 1,000times.
- Inside the loop, add an if statement: if die1 + die2 == 12,then add 1 to a counter.
- After the loop, display the result.
var die1 = -1;
var die2 = -1;
var loopNum = 0; //use to count number of loops
var twelveCount = 0; //use to count 12’s
while(loopNum < 1000){
loopNum++;
die1 = randomNumber(1, 6);
die2 = randomNumber(1, 6);
console.log(“Rolled a ” + die1 + ” and a ” + die2 + ” for atotal of ” + (die1 + die2));
}
console.log(“The number of times 12 was rolled was: ” +twelveCount);
console.log(“Done.”);
The starter code sets up the whole experiment for you, except itdoesn’t count the number of 12’s rolled – that’s your job. Note: Ifyou remove (or comment out) the console.log statement that displaysevery roll of the dice, the experiment will speed up A LOT! Youcould do tens of thousands of dice rolls in a matter ofseconds.
Expert Answer
Answer to (1 point) Using an if Statement In a Loop A common thing to do is to use variables to keep track of some sort of count…. . . .
OR

