1
00:00:00,180 --> 00:00:05,700
You might get a question, an interview right to function, which implements shuffle, what the shuffle

2
00:00:05,700 --> 00:00:10,170
means, you simply change the positions of elements inside an array.

3
00:00:13,500 --> 00:00:17,610
And actually, it's not that easy to implement it was using, for example, for each.

4
00:00:17,880 --> 00:00:22,830
But here we can leverage memorandum in order to sort elements randomly.

5
00:00:23,010 --> 00:00:24,470
This is actually what we want.

6
00:00:24,810 --> 00:00:27,870
So here let's create our shuffle items function.

7
00:00:28,110 --> 00:00:34,140
And the main idea is that here we're getting our array of items and this can be whatever it can be,

8
00:00:34,140 --> 00:00:34,920
just a number.

9
00:00:34,920 --> 00:00:38,100
It can be an object or maybe even array of arrays.

10
00:00:38,310 --> 00:00:40,260
It must work in any case.

11
00:00:40,500 --> 00:00:41,880
This is what we want to do.

12
00:00:41,920 --> 00:00:48,240
We want to look through every single element and create for every element and object whisky and value.

13
00:00:48,510 --> 00:00:50,060
And actually, they're inside.

14
00:00:50,070 --> 00:00:53,580
We will have some random key generated by random.

15
00:00:53,820 --> 00:00:55,620
And we also will have their value.

16
00:00:55,650 --> 00:00:57,990
This is exactly the value of our element.

17
00:00:58,350 --> 00:01:04,020
In this case, we can sort by this field, our array and then just remove this random key.

18
00:01:04,230 --> 00:01:05,160
Let's do this now.

19
00:01:05,280 --> 00:01:10,140
So I want directly here to return our items and first of to follow Colin map.

20
00:01:10,380 --> 00:01:13,140
And here we have access to every single element.

21
00:01:13,400 --> 00:01:15,900
Let's name it Tatum and what we want to do.

22
00:01:15,900 --> 00:01:20,160
We want for every single element to return an object with two properties.

23
00:01:20,400 --> 00:01:24,270
Let's name this property sword, because actually, this is a property for certain.

24
00:01:24,480 --> 00:01:26,700
And here we just write random.

25
00:01:26,910 --> 00:01:33,330
So here we will have our random property, and the next one will be value and inside value, restoring

26
00:01:33,330 --> 00:01:34,110
our item.

27
00:01:34,410 --> 00:01:39,450
So actually, when we check our code in browser now, so we're just calling our shuffle items, for

28
00:01:39,450 --> 00:01:45,270
example, was one and two, as you can see back where get into objects sold and here and the number

29
00:01:45,420 --> 00:01:47,790
and value is value of our item.

30
00:01:47,970 --> 00:01:53,280
And this is actually extremely good because here and say the value we can store numbers, objects or

31
00:01:53,280 --> 00:01:57,150
arrays, it doesn't actually matter for us after our mapping.

32
00:01:57,150 --> 00:02:02,160
We want to sort our elements inside this array by this certain property.

33
00:02:02,430 --> 00:02:05,310
This is where here we can write salt and we're getting here.

34
00:02:05,340 --> 00:02:06,960
Item one and item two.

35
00:02:07,200 --> 00:02:08,580
And here what we want to do.

36
00:02:08,580 --> 00:02:10,390
We want to write item one.

37
00:02:10,410 --> 00:02:13,350
The third minus item to the third.

38
00:02:13,620 --> 00:02:19,020
And actually, we know that SALT is a numeric property, which actually means here we can simply use,

39
00:02:19,020 --> 00:02:21,990
subtract and not write minus one and one.

40
00:02:22,200 --> 00:02:23,730
It will sorted correctly.

41
00:02:23,880 --> 00:02:29,130
But for us, or doesn't matter at all, because we just need two sources to change positions.

42
00:02:29,340 --> 00:02:32,700
And after we change positions, we want to get our values back.

43
00:02:32,730 --> 00:02:34,590
For this, we must write one more map.

44
00:02:34,740 --> 00:02:35,780
And here we have.

45
00:02:35,790 --> 00:02:40,860
Let's name a because this is our object and we want to read here a dot value.

46
00:02:41,100 --> 00:02:45,840
And actually, this is our item that was stored here inside every single object.

47
00:02:46,110 --> 00:02:48,630
Let's check the sour time to load in the page here.

48
00:02:48,640 --> 00:02:51,380
Shuffle items one two Where get into one?

49
00:02:51,570 --> 00:02:57,450
I'm calling it again here to one again here one two, which means it is working within the elements

50
00:02:57,600 --> 00:02:58,950
and it just shuffles it.

51
00:02:59,190 --> 00:03:04,590
And actually, this is an amazing way to solve this because we're leveraging here math, random and

52
00:03:04,590 --> 00:03:10,350
for every single element, we're creating this sorting and value and we simply saw this random numbers

53
00:03:10,500 --> 00:03:11,880
and now it is shuffled.

54
00:03:12,210 --> 00:03:15,030
And after this, we are just getting our items back.

55
00:03:15,210 --> 00:03:19,740
So this is the easiest and the most correct way to shuffle elements inside the ray.
