Question

In: Computer Science

Use a JavaScript program to: Stimulate a coin tossing environment using random functions and using a...

Use a JavaScript program to:

Stimulate a coin tossing environment using random functions and using a for loop - toss the coin 100 times and print the number of heads and tails.

A detailed answer would be appreciated, I would like to learn from it rather than just know the answer. Thanks in advance!

Solutions

Expert Solution

let heads = 0;  //variable to store heads value
let tails = 0;  //variable to store tails value
for(let i=0; i<100; i++){
    let random = Math.random(); //randomly generating number 
    if(random >= 0.5){   //condition to check heads or tails
        heads++;   // increase heads count
    }
    else{
        tails++;  // increase tails count
    }
}
console.log("heads = " + heads);
console.log("tails = " + tails);

In the first two lines heads and tails are variables to store heads and tails count.

Then in the third line we initialized for loop which run for 100 times.

In the forth line we initialized variable to store random number and that random number is generted from Math.random() function and this Math.random() will only generate number between 0 to 1 .

If you want to generate random numbers between specific number you have to use Math.floor(Math.random() * 10) . Number 10 in indicates we want to generate random number between 0 to 9 which are 10 values in total

And in fifth line we used if conditon to check if the random number is grater than 0.5 or not. if the condition is satisfieed then the code in curly braces is excuted or that code will be skippped

heads++ will increase heads count to +1 that is only if previous if condition is satisfied

If that if condition is not satsfied then the code in else curly braces will be executed

Finally console.log will print the value inside the brackets .And in the brackets if you ive value between quotes it will print as it is watever you give or you can print variable you intialized earlier by entering variable name (not in between brackets) .

If you want to print your variable value and enter a specific value you can use + operator to combine both

Note: Code after // is a comment compiler will not read it. it is just for developer information

This is my code and its output , I used Visual studio to write and run code


Related Solutions

In C++  Write a program that simulates coin tossing. For each toss of the coin the program...
In C++  Write a program that simulates coin tossing. For each toss of the coin the program should print heads or tails. Let the program toss the coin 100 times and count the number times each side of the coin appears. Print the results. 0 represents tails and 1 for heads.
Write an application that simulates coin tossing. Let the program toss a coin each time the...
Write an application that simulates coin tossing. Let the program toss a coin each time the user chooses the “Toss Coin” menu option. Count the number times each side of the coin appears. Display the results. The program should call a method flip( ) that takes no arguments and returns a zero to represent a tail or a one to represent a head. There is a Random class that will allow you to generate a random integer. Import it from...
in tossing a coin 1024 times we get 460 tails. is this coin fair? use a...
in tossing a coin 1024 times we get 460 tails. is this coin fair? use a significance level equal to 5%
Write a program that determines the probability of tossing a coin 10 times and getting exactly...
Write a program that determines the probability of tossing a coin 10 times and getting exactly 0, 1, 2, 3, etc. heads. This is the binomial probability distribution. Store the probability in an array. You could get 0 heads or 10 heads or anything inbetween. Use a for loop. The for loop will go from 0 to 10 inclusive. Use r as the number of successes. So r will go from 0 to 10. The probability of a success is...
USING jgrasp environment, javascript rite a complete program that prompts the user for two integers and...
USING jgrasp environment, javascript rite a complete program that prompts the user for two integers and then prints the following report to the screen, exactly as shown. example, using 23 and 18 as the user input: your numbers are :23 and 18 sum=41 product=414 use the scanner class nextlnt() method to read the user's numbers: scanner keyboard... ...keyboard.nextlnt()
Write the below code to use HTML and JavaScript. 1. a) Write a JavaScript program to...
Write the below code to use HTML and JavaScript. 1. a) Write a JavaScript program to display the current day and time. b) Write a JavaScript program to print the contents of the current window.   c) Write a JavaScript program where the program takes a random integer between 1 to 10 d) Write a JavaScript program to calculate multiplication and division of two numbers (input from the user). e)Write a JavaScript program to create a new string from a given...
Python - Rewriting a Program Rewrite Program 1 using functions. The required functions are in the...
Python - Rewriting a Program Rewrite Program 1 using functions. The required functions are in the table below. Create a Python program that will calculate the user’s net pay based on the tax bracket he/she is in. Your program will prompt the user for their first name, last name, their monthly gross pay, and the number of dependents. The number of dependents will determine which tax bracket the user ends up in. The tax bracket is as follows: 0 –...
Write a program using multiple functions. Make use of an array to store data Make use...
Write a program using multiple functions. Make use of an array to store data Make use of searching techniques Read data from a file Write data to a file Instructions: In this lab, you will be examining a set of stock collected over a twenty four day period. Be sure to make use of an array to store these stocks. You will be required to read in the data points from a file. Write a function to read in the...
-create a magic 8 ball program in JavaScript - use a loop to ask for the...
-create a magic 8 ball program in JavaScript - use a loop to ask for the question - use a random number to get the answer let randAnswer=Math.round(Math.round()*10); - must have at least 10 different answers -Must use either elseif or switch statement for answer -must output both answer and user input to console - the program should repeat indefinitely until either blank input, or cancel is selected Bug in jsbin.com Please don't use HTML thanks
JavaScript Task Load three images of colorful fall leaves and using functions with parameters do the...
JavaScript Task Load three images of colorful fall leaves and using functions with parameters do the following. (Remember you can call the same function multiple times sending the function different values.) When you move the mouse over the first image, have the second image change to a pumpkin. When you move the mouse off of the first image, have the second image change back to the original leaves. When you move the mouse over the second image, have the third...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT