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%
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...
Using JavaScript You must submit a file, called indentation.js indentation.js: program to indent The following program...
Using JavaScript You must submit a file, called indentation.js indentation.js: program to indent The following program is difficult to follow because of its indentation: print ( "binary"); var n = Math.pow (2, 31) - 1; var bits = 0; while (n> 0) { bits + = n & 1; n >> = 1; } print ("Number of bits at 1:" + bits); for (var i = 1; i <= bits; i ++) { var str = ""; if (i% 3...
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...
-create a magic 8 ball program in Javascript. -use a loop to ask for the question...
-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.random()*10); -must have at least10 different answers -must use elseif or switch statement -must outputs both answers and user to input the console -program should repeat indefinitely until either blank input or cancel is selected
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT