You might get a question, an interview right to function, which implements shuffle, what the shuffle
means, you simply change the positions of elements inside an array.
And actually, it's not that easy to implement it was using, for example, for each.
But here we can leverage memorandum in order to sort elements randomly.
This is actually what we want.
So here let's create our shuffle items function.
And the main idea is that here we're getting our array of items and this can be whatever it can be,
just a number.
It can be an object or maybe even array of arrays.
It must work in any case.
This is what we want to do.
We want to look through every single element and create for every element and object whisky and value.
And actually, they're inside.
We will have some random key generated by random.
And we also will have their value.
This is exactly the value of our element.
In this case, we can sort by this field, our array and then just remove this random key.
Let's do this now.
So I want directly here to return our items and first of to follow Colin map.
And here we have access to every single element.
Let's name it Tatum and what we want to do.
We want for every single element to return an object with two properties.
Let's name this property sword, because actually, this is a property for certain.
And here we just write random.
So here we will have our random property, and the next one will be value and inside value, restoring
our item.
So actually, when we check our code in browser now, so we're just calling our shuffle items, for
example, was one and two, as you can see back where get into objects sold and here and the number
and value is value of our item.
And this is actually extremely good because here and say the value we can store numbers, objects or
arrays, it doesn't actually matter for us after our mapping.
We want to sort our elements inside this array by this certain property.
This is where here we can write salt and we're getting here.
Item one and item two.
And here what we want to do.
We want to write item one.
The third minus item to the third.
And actually, we know that SALT is a numeric property, which actually means here we can simply use,
subtract and not write minus one and one.
It will sorted correctly.
But for us, or doesn't matter at all, because we just need two sources to change positions.
And after we change positions, we want to get our values back.
For this, we must write one more map.
And here we have.
Let's name a because this is our object and we want to read here a dot value.
And actually, this is our item that was stored here inside every single object.
Let's check the sour time to load in the page here.
Shuffle items one two Where get into one?
I'm calling it again here to one again here one two, which means it is working within the elements
and it just shuffles it.
And actually, this is an amazing way to solve this because we're leveraging here math, random and
for every single element, we're creating this sorting and value and we simply saw this random numbers
and now it is shuffled.
And after this, we are just getting our items back.
So this is the easiest and the most correct way to shuffle elements inside the ray.