There are JavaScript methods that you can take advantage of when you need to. We like to propose applications where you can use such methods. This helps you to understand them better. In this lesson today we are going to use a JavaScript method of push to show a random number.
Set The JavaScript Array
Lets set up the JavaScript array. We are going to use an example of the Power Ball Lottery. This consists of 6 numbers total. The first 5 numbers are 1 through 69. However the last number is 1 through 26. We are going to set up the first 5 numbers for our array.
var lucky = ["11", "25", "33", "42", "57"];
Generate a Random Number Math Function In JavaScript
To generate a random number from 1 to 26 we will use this JavaScript function stored in a variable called rand.
var rand = Math.floor(Math.random() * 26) + 1;
Use Push() Method To Add Final Number
Use the push method to add the final number to the array. This will be a number auto generated 1-26. Here is the syntax.
lucky.push(rand);
Join Method With JavaScript Arrays
You can use the join method to input something between each data item in your array.
var fruits = ["Tom", "Dick", "Harry"]; var x = document.getElementById("foo") x.innerHTML = fruits.join(" / ");
Pop Method in JavaScript
Use the pop method when you want to remove the last item in the array.
Return Items Popped Out
There is also a way to return the item that was popped out.
A1WEBSITEPRO Social Media Pages
Here are my social media pages, lets hook up!