We have an array where we need to select a random value from that array.
Currently, we have three different items within that array, three different text strings, and we
possibly could have more.
So we want to make this as dynamic as possible as well as we don't want the user to know which element
and which string value we're going to be outputting.
And this is where we turn to Amarth random.
This is a function in JavaScript that returns a number in the range of zero to one inclusive of zero,
but not of one.
So basically you're going to get return back a value that's zero decimal and a whole string of numbers.
And here's some examples of what happens and how the output looks within math.
Random.
So over here, we're just simply got a math random and this is a typical number that gets returned.
So if we do want to have a random number, that's a particular value that we need to multiply that number
by the value that we want to have output.
There's an example here of how to get a random integer.
So minimum, maximum.
So we've got minimum is the math seeling.
Maximum is the math flaw plus minimum.
So essentially what we're doing is we're doing math for multiplying it by math, random, and that's
returning back the number.
So let's go ahead and take a closer look at this within our ED.
So going into play the game, first of all, let's create a random number.
So we'll call it random number using math.
I don't see what that's going to return, so a console log out the value of a random number, so refresh
and every time we hit start, we get a random number.
So it's got a lot of decimal.
It's decimal with a lot of numbers.
So we want to actually multiply that by a meaningful number.
And if we want to have a number from zero to nine, we could multiply it by 10.
So that would give us a random number, anywhere from zero to nine, and just to be sure, this is where
we could do math, fluorosis, another builtin not function in JavaScript, and that would actually
bring the returned value down to the floor to make sure that we're rounding it down to the nearest floor
value.
And as we saw over here, when we use floor, then this is the rounding a down to the nearest full number.
Whereas if we do math ceiling, we're rounding up to the nearest full number.
So that's what the difference there is.
So right now, if we press it multiple times, you're going to see that we're always returning the number
back anywhere from zero to nine.
So there's nine.
And if we keep going, we see that we've got a zero.
So we never get 10.
If we do want to have 10 that we can add plus one, this will return back a number from one to 10.
And as you can see, 10 got returned back there.
And if we want to get a random item from our array, we know that this is an array and arrays have length.
So instead of 10, we could do wording Dorte length and this is going to be equivalent to three.
I'm going to get rid of that zero one because arrays are zero based and we want to actually return a
number back anywhere from zero to two.
And as you can see, the numbers being returned back are always within that range, within a random
number.
You could try it out for yourself as well.
So the next thing that we want to do is output within that message.
So taking message and updating the inner text of message.
We're going to use the wording, ouray.
And specify an index value and that index value is going to be equivalent to that random value.
So essentially, as we saw, we're getting a value of zero one or two because we have three items within
the array.
So that's indicated by that length index values from the array.
And this is dynamically so in case our rate increases in length, our formula will always still work.
So go ahead and try it out for yourself.
That's not random and try some of the examples, generate some random numbers and output in the console.
And coming up next, we'll show you how you can update the start button as well as how we can set a
start time when the user presses the start button.
And then another time that we can get when the user completes typing and hits the end button.
So all of that is still yet to come.