207k views
4 votes
What does the following code segment do?

var count =0;
var sevens =0 ;

while(count<100)
{
var roll1 = Math.floor(Math.random()*6+1);
var roll2 = Math.floor(Math.random()*6+1);

if(roll1 + roll2 == 7)
sevens = sevens +1;

}

1 Answer

2 votes

Answer:

Hi!

var count =0; //initialize count.

var sevens =0 ; //initialize sevens.

while(count<100) // loops while count is minor than 100. *counts never add 1 at the final of the loop, so this while is always true.

{

//Math.floor(x) round to the max Integer a number. Example : 45.90 -> 46.

//Math.random() returns a number between [0, 1).

var roll1 = Math.floor(Math.random()*6+1); //Gets a integer using Math.random(), adds 1, and round it withMath.floor() then saves in roll1.

var roll2 = Math.floor(Math.random()*6+1); //Gets a integer using Math.random(), adds 1, and round it withMath.floor() then saves in roll2.

if(roll1 + roll2 == 7) //If the sum of roll1 and roll2 is 7 adds 1 to sevens.

sevens = sevens +1;

}

//*count is not incremented, so while(count<100) -> always true.

User Creanion
by
4.9k points