Answered step by step
Verified Expert Solution
Link Copied!

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Financial management theory and practice

Authors: Eugene F. Brigham and Michael C. Ehrhardt

12th Edition

978-0030243998, 30243998, 324422695, 978-0324422696

Students also viewed these Programming questions

Question

American colonies independent from the British rule ?

Answered: 1 week ago

Question

Who was the principles author of the declaration of independence?

Answered: 1 week ago

Question

Main issue that caused the American civil war?

Answered: 1 week ago

Question

Which event marked the official end of the American civil war ?

Answered: 1 week ago