Hello there.
How's it going in this lesson, we are going to be creating an active element that's going to give us
an objective within our gameplay.
So creating a function would create a function for that, creating a function we could just call it
make active.
And this is going to select one of the elements, one of the squares at random and apply a class of
active to that to that element.
So creating a random index, we're going to use math, science, math flaw.
So bring the math value down to zero.
And this is just regular JavaScript math, random method that gives you a random number and not random,
gives you a number that zero decimal and a whole bunch of digits.
If you do math lower than it rounds it down to the nearest whole number.
So going down to the full number and then we just need to specify, we want to multiply it by and because
we've got our squares and as we know, squares is an array that we do have a value of and we have a
length of.
So we can utilize that length and we see it comes up at twenty five.
And remember, we need to keep this dynamic and we actually have twenty four elements that are visible.
So we need to make a slight adjustment here.
But let's use squares lengths in order to get a random value.
So now with this formula we do have a possibility of having zero and we don't want zero.
So let's do a condition here to check, to see what the value is and checking to see if random index
is not equal to zero.
So making sure that if we do by chance get a zero, that one in 25 chance it's a zero, then we're just
going to do the function again.
So we do our statement here and launch make active again.
So it'll keep doing the make active until we get the result that we want.
So looping through the function until we finally get a value that's not conflicting with what we're
looking for.
There's also something else that we need to make to look at.
And we don't want the new active object to ever end up to be the place, the same place as the player
square value.
So this is where we're going to start using that player square value.
And we want to make sure that that is not equal to the random index value.
And if both of those are good, then we are ready to generate our position using that random index.
So first of all, let's see what number we can pull out.
And because this is a function, we can run that within our console.
So typing in that make active, you see that we're selecting one of those elements there and we're turning
back fourteen.
If we do it again, we get 11, 16 and so on.
So we can use this in order to return back one of those squares.
And remember that squares array that we have that contains all the elements.
This is where we can use that random index value that we're just generating.
And because this is referring to the element, we can update that class list and add the class of active
to it.
So see how that works.
And if you wanted to see which element we're selecting and we want to output that, we can put that
in the console instead to have other than the number.
So now when we do make active function, we generate randomly one of the squares turns green.
So that's our objective of the game, is for the player to be able to move over those squares.
And of course, we're only going to have one.
And whenever the player moves over the square, then we're going to update it.
And that's going to score a point.
And as you can see it, they're disappearing.
So we need to account for that and we can do all of that in the upcoming lesson.
So for now, add in a function that can make one of the elements or a bunch of elements active, make
sure that it's within the spectrum of elements that are available, as well as make sure that we're
not overlapping the current square that the player is on.
So add that into your project.
You're going to be ready to complete this within the next lesson where we're going to be creating these
objectives within our functions and then checking to see if the player is on top of the correct cell.
So that's still to come.