Now that we have a better understanding of how javascript works it would be a good time to work on a
couple of projects.
First we'll start with a simple script that changes the background color of a web page every time the
page is refreshed.
We can see the end result in this example every time I press the refresh button.
You can see that the background color is changing so let's go ahead and get started.
The first thing you need to do is set up your basic HDMI page structure and save the file on your computer.
Next create your script tags inside the body tags inside our script.
We need to define a variable that stores all the possible background colors in an array.
So here we have a variable called BMG color.
And it stores eight values inside an array and each value is a hex code representing a different color.
You can use the same colors we've used or different colors if you like.
Now we need to create the javascript statement that will randomly select one of the colors in our array
and apply it as the background color.
Each time the page is refreshed so to do that we've used the document body style property and set it
to a randomized value in the BGP color array index the value of the index is derived by multiplying
a random value between 0 and 1.
And that's done using the math dot random statement we've multiplied it by the length of the array which
is eight.
We then use the math dot floor statement to round the value to the nearest whole number.
So this gives us the value of our index number in the color array.
And each time it's going to be different so that means each time the background color will be different
as well and that's it for the script.
Once you're done just save the file and preview it in your web browser.