TIP: If (for some reason) you want to make sure that no number ends up in the place it was in before the shuffle, do this minor change in the code. ``` function shuffle(array) { for(let i = array.length - 1; i > 0; i--) { const random = Math.floor(Math.random() * i); // `i` instead of `i+1` [array[i], array[random]] = [array[random], array[i]]; } } ```
// Fisher-Yates algorithm
const cards = ['A', 2, 3, 4, 5, 6, 7, 8, 9, 10, 'J', 'Q', 'K'];
shuffle(cards);
console.log(cards);
function shuffle(array){
for(let i = array.length - 1; i > 0; i--){
const random = Math.floor(Math.random() * (i + 1));
[array[i], array[random]] = [array[random], array[i]];
}
}
Bro can you do Kali Linux ?
helped me a lot! thanks for the tutorial
Beautiful ❤
oh my god bro this is so helpful! thanks for saving me 25 lines of spaghetti code!
Enjoy the show☺
Day 3 ends…. I’ll go check more about reduce()
Could you create a tutorial about Rust programming language?
TIP: If (for some reason) you want to make sure that no number ends up in the place it was in before the shuffle, do this minor change in the code.
```
function shuffle(array) {
for(let i = array.length - 1; i > 0; i--) {
const random = Math.floor(Math.random() * i); // `i` instead of `i+1`
[array[i], array[random]] = [array[random], array[i]];
}
}
```
How is this more efficient than the first way? Just because of the built in algorithm for the sort() method?
Just a random comment to shuffle the RUclips algorithm🎉
Bro request course lua
bro i am following your previous js playlist should i continue that or should i continue from this series
probably this one is better (but you might be done with both by now lol)
Yes Javascript🃏
best
What kind of project we use this on?
May be some card game, or anything where you need to shuffle an array to create a different/random scenario everytime.
bro please elaborate a lil bit more of JS topics...
What a stupid language. But explanation is good.
Bro, how many topics about JS left?