Allows user to simulate the lottery by picking numbers and playing Powerball! They can play Powerball at zero the cost and probably the same amount of winnings as real Powerball (zero. because winnning the lottery is impossible). Can be found here: http://power-ball.herokuapp.com
- © 2021 GitHub, Inc.
- Contact GitHub
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.Powerball Simulator. Contribute to rbtnguyen/powerball development by creating an account on GitHub.
Jun 30, 2018 · 9 min read
Step 1: Setting Up the HTML
Step 2: Creating the CSS stylesheet
To add the CSS stylesheet, create a new blank file in your text editor of choice and just copy and paste the following code below into a new file and name it style.css.
We need to begin creating variables and assigning these variables a value that will generate random numbers. You use variables to store, retrieve, and manipulate values that appear in your code. Declaring a variable is simple to do. Begin your statement with a var and add an appropriate name that will be placed after the var. Once you have the var name established, assign it a value by using an equals sign.
e.g., var num = Math.floor(Math.random() * 101);
If you want to create a random number between 0 and 100 you could use the floor function to round down.
The floor method cuts off everything past the decimal, leaving only the integer portion. The random number generated from the code above would generate numbers between 0 to 99. However, if you want a random number between 0 and 100, you would use: Math.floor(Math.random()*101);. In our new Powerball game we will be entering digits between 0 and 100. This means we are going to use: Math.floor(Math.random() * 101);
Step 4: Creating the Function
Now that you know how to create a random number, it is time to get down to business and start building our code to function like a Powerball simulator. All the instructions for our Powerball simulator is going to be defined inside of a function which lies within the opening and closing curly brackets . We will call this function playGame.
Now it’s time to look at how we will set up the source code and explain what is going on here. The first part of the function defines the random numbers and then it will compare those numbers to the user’s guesses. Notice all seven variables declared for the user’s guesses is highlighted in the yellow box from lines 17–23.
In order to compare the numbers that the user enters you must parse the number into an integer. Anytime you have a user fill out an input field in the HTML it is entered as a string value. To compare an integer, it must be equivalent to another integer. An integer value is not equivalent to a string value. This is why it is so important to use the parseInt for each guess the user enters. Our code is now structured appropriately and will help us come up with seven random numbers to compare with.
The most important part of this entire game is the ability to compare the numbers entered with the random numbers generated. This is what we call conditional operators. It will compare the user’s guess to see if there are any matches and output the results into the HTML. We have to create conditional operators to compare the amount of numbers that match and have it output a message if there is a match, such as “You Lose!”, “You Win!”, or even something to the effect of “You have 2 matches!”. This is where you can have fun and come up with your own creative way of handling this game.
Conditional statements are used to perform different actions based on different conditions. In this project I am using the if…else statement to compare the random number with the user’s number.
e.g., if(num1 === userNum1 || num2 === userNum2) >.
Be sure this is placed outside of the opening and closing curly brackets of your function! Here is an example to help demonstrate what I mean:
Step 5: Play the Game!
You now have a basic Powerball simulator. Be sure to hit the play button several times over. You just might get more than three numbers matching at any given point.