I am watching fCC’s new Javascript Games video course and am on the first game, a memory game. I am confused at what this line of code means.

cardArray.sort(() => 0.5 - Math.random())

I am watching fCC’s new Javascript Games video course and am on the first game, a memory game. I am confused at what this line of code means.

cardArray.sort(() => 0.5 - Math.random())

This looks like it (weakly) shuffles the elements in `cardArray`

.

Here’s the MDN page about the sort method’s callback function.

The sort function goes through the array in order, looking at pairs of elements.

When the callback returns a positive number, that pair of elements are swapped.

Otherwise, they stay where they are.

```
() => 0.5 - Math.random()
```

This function is simply creating a random chance of getting a positive or negative number, because `Math.random()`

returns a number from 0 (inclusive) to 1 (exclusive). Half the time, it will evaluate to a negative number, and half the time it will evaluate to a positive number.

In terms of how that works with sort, half the time the elements will be swapped, and half of the time they won’t.