144k views
3 votes
What does the following code segment do?

var isSeven =false;

while ( isSeven==false )
{

var roll1=Math.floor(Math.random( )*6+1);

var roll2=Math.floor(Math.random( )*6+1);

if(roll1 + roll2 ==7)
{
isSeven = true;
alert(roll1 +"--"+roll2);
}

}

It keeps generating two random numbers of any values until the sum of the two numbers is 7.

It keeps generateing two random numbers between 1 and 6, and adds up the two numbers.

It keeps generating two random numbers between 1 and 6, until the sum of the two numbers is 7.

It keeps generating two random numbers between 1 and 6, then check if the sum of the two numbers is 7.

User Brildum
by
6.9k points

1 Answer

4 votes

Answer:

It keeps generating two random numbers between 1 and 6, until the sum of the two numbers is 7.

Step-by-step explanation:

isSeven is always false until the sum of roll1 and roll2 is seven.

Math.random() generates a number between 0 and 1 where 1 is exclusive and 0 is inclusive and multiplying it with 6 means that it will generate the number between 0 and 5 and adding 1 means a number between 0 and 6.

Math.floor() is to consider the lower bound integer of the float value for 1.9 it will consider 1 only.

User Vigor
by
7.5k points