Question

In: Computer Science

JavaScript Programming Assignment PLEASE NOTE:  You must create and call a main function, and if instructed include...

JavaScript Programming Assignment

PLEASE NOTE:  You must create and call a main function, and if instructed include additional functions called by the main. Make sure to use ES6 style of keywords => instead of the older function and for local scope variables use the keyword let and not a keyword var. Make sure to follow the requirements and recheck before submitting.

PROJECT GOAL:

Create a program that simulates tossing a coin. This program should be titled flippingacoin.js and will require you to include two functions, one of them is called main and the other flip. The use of a loop is required for this program. Make sure the program includes a user prompt asking how many times to toss the coin. Program a function called flip which does not include parameters and randomly (using math library random) returns either a String called heads or one called tails. Call onto this function in main as many times as requested by using a loop. Make sure to report the results.

Example output:

How many times should I toss the coin? 4000

Results of 4000 tosses.

Heads: # 2635, Tails: 1365

NOTE: End users should be able to enter a higher number of tosses such as 300500020 and receive accurate results.


Thank you for your help with this project.

Solutions

Expert Solution

// Javascript program that simulates tossing a coin.

let readline = require('readline-sync'); // module for taking input

// flip function that returns "heads" is random() returns a value <0.5 else "tails"
// simulates the flipping of a fair coin where probability of heads and tails are equal
var flip = () =>{
   if(Math.random() < 0.5)
       return "heads";
   else
       return "tails";
}

// main function that includes a user prompt asking how many times to toss the coin.
// and records the number of heads and tails for that number of coin toss
var main = () =>{
  
   //input of number of coin toss
   let num_toss = parseInt(readline.question("How many times should I toss the coin?"));
   let i;
   let num_heads = 0, num_tails = 0; // counter to store the number of heads and tails
   // loop to simulate the coin flip
   for(i=0;i<num_toss;i++)
   {
       let result = flip(); // get the result of one flip
       // if we get heads increment the number of heads
       if(result === "heads")
           num_heads++;
       else // increment the number of tails
            num_tails++;
   }
   // output the result
   console.log("Results of "+num_toss+" tosses.");
   console.log("Heads: # "+num_heads+", Tails: "+num_tails);
}

// call the main function
main()

//end of program

Output:


Related Solutions

JavaScript Programming Assignment PLEASE NOTE:  You must create and call a main function, and if instructed include...
JavaScript Programming Assignment PLEASE NOTE:  You must create and call a main function, and if instructed include additional functions called by the main. Make sure to use ES6 style of keywords => instead of the older function and for local scope variables use the keyword let and not a keyword var. Make sure to follow the requirements and recheck before submitting. PROJECT GOAL: Assume that hot dogs come in packages of 10, and hot dog buns come in packages of 8....
Code programs using ReadlineSync for prompts. Note: For all programs, create and call a main function,...
Code programs using ReadlineSync for prompts. Note: For all programs, create and call a main function, and if required additional functions called by the main. Also please use the ES6 style of keywords => instead of the older function and for local scope variables use the keyword let, not var triangle.js Write a program that is required to use nested loops to generate a triangle as shown in the sample run below. The program should begin by prompting the user...
Code programs using ReadlineSync for prompts. Note: For all programs, create and call a main function,...
Code programs using ReadlineSync for prompts. Note: For all programs, create and call a main function, and if required additional functions called by the main. Also please use the ES6 style of keywords => instead of the older function and for local scope variables use the keyword let, not var Name: coinflip.js For this program you will have two functions, one called main and the second called flip. This program is also required the use of a loop construct. Write...
Code programs using ReadlineSync for prompts. Note: For all programs, create and call a main function,...
Code programs using ReadlineSync for prompts. Note: For all programs, create and call a main function, and if required additional functions called by the main. Also please use the ES6 style of keywords => instead of the older function and for local scope variables use the keyword let, not var Name: cookout.js Assume that hot dogs come in packages of 10, and hot dog buns come in packages of 8. Write a program called cookout.js, that calculates the number of...
Using HTML and JAVASCRIPT Create a Content area on the main entry page will include inputs...
Using HTML and JAVASCRIPT Create a Content area on the main entry page will include inputs for customer entry for the following: Entry field (with labeling) for the Customer's name Entry field (with labeling) for the Customer's email address Entry field (with labeling) for which room the customer is planning on painting Entry field (with labeling) for the width of the room Entry field (with labeling) for the length of the room Entry field (with labeling) using a colour box...
You will be writing the function definition and the main function with the function call. Be...
You will be writing the function definition and the main function with the function call. Be sure to write the function definition below the main function. The function prototype has been provided, you may add the prototype to you answer. You do not need to add the preprocessor directives like #include<stdio.h>. Write the function definition for UpdateGrade . The function will take two arguments, a double called originalGrade a pointer to a double called newGradePtr. This function will have a...
The main tasks of this C++ programming assignment include implementing a transformation matrix and a view/projection...
The main tasks of this C++ programming assignment include implementing a transformation matrix and a view/projection matrix. Giving three 3D points v0(2.0, 0.0, −2.0), v1(0.0, 2.0, −2.0), v2(−2.0, 0.0, −2.0),you are required to transform these points to the camera/view/monitor coordinates system, and draw a lined triangle based on them get_projection_matrix(float eye_fov, float aspect_ratio, float zNear, float zFar): using the giving parameter, build a projection matrix, and return it. Here is what I have so far. Eigen::Matrix4f get_projection_matrix(float eye_fov, float aspect_ratio,...
only JAVA code /** Create a method as instructed below and then call it appropriately. */...
only JAVA code /** Create a method as instructed below and then call it appropriately. */ import java.util.Scanner; public class MoreBankCharges { //Constant declarations for base fee and per check fees //Class scope so constants are accessible by all methods static final double BASE_FEE = 10.0; static final double LESS_THAN_20_FEE = 0.10; static final double TWENTY_TO_THIRTYNINE_FEE = 0.08; static final double FORTY_TO_FIFTYNINE_FEE = 0.06; static final double SIXTY_OR_MORE_FEE = 0.04; public static void main(String[] args) { //Variable declarations int numChecks;...
/** Create a method as instructed below and then call it appropriately. */ import java.util.Scanner; public...
/** Create a method as instructed below and then call it appropriately. */ import java.util.Scanner; public class BankCharges { public static void main(String[] args) { //Variable declarations int numChecks; double perCheckFee; double totalFee; double totalFeesAllAccounts = 0; //accumulator int numAccounts;    //Constant declarations for base fee and per check fees final double BASE_FEE = 10.0; final double LESS_THAN_20_FEE = 0.10; final double TWENTY_TO_THIRTYNINE_FEE = 0.08; final double FORTY_TO_FIFTYNINE_FEE = 0.06; final double SIXTY_OR_MORE_FEE = 0.04;    // Create a Scanner...
For this assignment you need to create a ToDo list using Javascript, along with HTML and...
For this assignment you need to create a ToDo list using Javascript, along with HTML and CSS. Begin by creating a HTML page called todo.html. Then create a Javascript file called todo.js and link it in to the HTML page using a script tag. All Javascript for the assignment must be in the separate file. (For CSS, feel free to include styles in a style block at the top of the HTML page, or to link in CSS from a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT