Answered step by step
Verified Expert Solution
Question
1 Approved Answer
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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started