Ask Question
20 February, 00:32

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;

}

+4
Answers (1)
  1. 20 February, 02:05
    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 and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers