Great job on making it this far into the U.S. You've made it to your first challenge of the section
and that's to create a random array message.
And what we've been looking at in the earlier lessons with random is ideal in order to select random
items from the array.
So selecting the index, we can randomly select any value randomly from the array.
So we create an array, have a bunch of items in that array, and then selecting out a value that randomly
from the array and then output it within the document.
So appending the child to creating a text node in the depending it to the body.
Pause the video here and I'll walk you through the solution.
Great, I hope you had an opportunity to try this out, so first thing that we need is an array.
This can be an array of anything you want.
In this case, I'm going to have some hello messages and a welcome message and also bye bye message.
And we don't know which one we're going to get because this is going to be returned back at random.
Let's create a function in order to select an item from random and then we want to pass in the array.
So again, trying to make it dynamic as possible, because I know we've got the array out here, but
it's always better to be able to pass it in.
So that gives you more flexibility.
If you've got multiple arrays, you can use the same function in order to generate and return back a
random item from the array.
So it using the array object, we're returning back a value one of the index values.
So if we want to return back one there, you can see that when I refresh and a random item and in this
case it's passed the array, you can see that we get welcome being returned back because welcome is
index one.
And we also just saw that we could generate a random value.
So let's create temporary value and using a math flaw.
So always use math flaw and math random in combination and then math random.
And what do we want to multiply math random by?
We want to multiply it by the length of the array.
So we don't want to we don't want to avoid as much hard coding as possible.
And math flaw is ideal when you're working with arrays because arrays are zero index.
So zero one two and we're not going to have three.
So that's where my flaw brings that down.
So we're turning back whatever the index value is, or replace one with the index and move that in.
Let's clean that up and refresh.
And now whenever I select a random number, don't forget, we need to pass in the array.
We can get a random item coming from the array.
So there's another part to this challenge, and that was to use this function and output the content
into our HTML so that it's visible.
So every time we load the page, we're going to have a different value there.
So let's create another variable here.
We can also call that temp because this is out of the scope of the function and using the random item
passan the array object.
So now temp will have any one of the values that's being returned message.
So document.
So creating a text node and the contents of the text node will be whatever is being returned back from
the function and then using document body, we can append the child to the body so we can do is append
the message.
So now whenever we refresh, you can see that you've got this random value and this is coming out of
the array.
So doesn't matter how many times you refresh it, you're always going to get something different.
Well, of course, sometimes you do get the same one because we only have the three items in the array.
So try this out.