Question: 1.The following JavaScript code is intended to simulate flipping a coin 10 times and counting the number of HEADS that was flipped. It does not

1.The following JavaScript code is intended to simulate flipping a coin 10 times and counting the number of HEADS that was flipped. It does not work as intended. Explain the error (what the program actually does) and how you would fix the code to work as intended.

var flip, numHeads;

for (var i = 0; i < 10; i++) {

numHeads = 0;

flip = Math.floor(Math.random() * 2 + 1);

document.write(flip + " ");

if ( flip == 1 ) {// 1 represents HEADS

numHeads = numHeads + 1;

}

}

document.write(" " + numHeads);

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!