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
Get step-by-step solutions from verified subject matter experts
