Ask Question

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;

}

+3
Answers (1)
  1. 9 May, 20:26
    0
    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 always true.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “What does the following code segment do? var count = 0; var sevens = 0; while (count ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers