Within this lesson, we are randomizing the contents of an array and then we're going to be returning
back that array, newly, freshly created array that we are building out, we created a function already
called random array, and it's expecting an argument.
And that argument that it's expecting is an array that gets passed in there.
So now we're going to have our random array and will update this to be random or and then we can output
and console, lagrand this word, the string value that's turned into an array.
We can see that here in the right hand side in the console, reorganize the contents of the array.
Let's set up our loop to loop through the contents of the array, set up a variable called AI and we're
going to equal AI to the value of our array length, subtracting one quasar array length.
So now we get the index values of the items in the array.
Thus we need to subtract one and we're going to loop through while AI is less than zero.
So once it hits zero, then that's where once it goes below zero, then that's where we stop the loop.
And as long as I is above zero and that means that we need to decrease the value of AI.
So a lot of the loops that we've done before are going to be increasing and in this case we're going
to be decreasing that value.
And we're also going to need to set up a temporary value for that item within the array so that we can
store that and take that out and then store that as a new value as we build out the new temporary array.
And for now, what I'll do is I'll consult, logout the value of temp.
We also need to generate a random value, so called that G using what we did before with math flaw,
that we're going to multiply it by whatever the value of AI is plus one.
And remember, AI is decreasing in value as we're iterating through and a at the values of AI as well.
And we'll consult log out the value of G so that we can see everything that's happening here.
So as we Head Start, start out with the last letter and as we loop through and iterate through, AI
is decreasing in value and we're also getting our random different value every time we're decreasing.
So we can try that one more time and you can see that temp.
So that temp is the letter that we're on.
AI is the current index value that we're looking at.
So that's what we're getting E from.
And then G is the new random index value that we want to add into the array.
So let's build out our new array.
And we're just swapping the order between the two different items in the array, so we're taking the
value of I and in this case, we're going to take E and we're going to equal E to whatever the value
of J is.
And in this case, it would be the second item in the index.
So we're equaling those and then we're taking the value of J and we're equaling it to the value of temp.
So basically we're doing a quick swap over of these characters as we're iterating through, and that's
swapping around of the order of the characters is going to reorganize that current array.
And then lastly, what we're going to do is we're going to return that array.
And I would put that as a return value.
So let's try that out and hit start.
And you can see that now we've got a reorganized sorting of the contents of that array.
And so this is a website and we've reorganized the items in the array and now it's just a matter of
turning it back into a string value.
So right now, we're getting a return to value and we can use joint.
So we're joining it together with that's double two double quotes.
And now when we refresh, we hit start and we get a scrambled version of whatever word was sent into
the scrambler and this randomising of the array order.
This is a function that I used very often was in a lot of different applications.
So it's a very useful function, creating a random value and then swapping it with whatever that randomized
item was.
And it's doing it whatever number of characters we have in the array, essentially reorganizing the
array content.
So go ahead and try this out for yourself.
Add in the ability to randomize the order of an array and that's going to move us along to the next
step where we're almost ready to output that word to the player so that they can start guessing what
the word is that we've generated.
So go ahead and give it a try.