How to shuffle an array in JavaScript

preview_player
Показать описание
In this tutorial, we'll look at how to shuffle an array in JavaScript.

If you have an array in JavaScript that has any type of data in it, you can shuffle up the position. of the items in the array in a couple of ways.

In the video, I will show you first of all the easies way to shuffle an array in JavaScript using a simple custom sort function. This works well for most situations but there is some evidence that this might not be entirely random.

So once we've looked at the basic array shuffle in JavaScript, we'll look at a recommended algorithm for shuffling arrays, the Fisher Yates Algorithm.

To do a Fisher Yates shuffle in JavaScript we simply loop through the array and swap out items at random positions. Although not too complicated, the Fischer Yates JavaScript implementation is a few more lines of code and not as succinct as using a custom sort function.

Either way is fine depending on your needs!

#JavaScript #JavaScriptHowtos #ArrayShuffle Channel Handle @codebubb
Рекомендации по теме
Комментарии
Автор

hi, is there a possibility to implement this but with colors?
lets say theres 3 black circles. I would like the 3 black circles to randomize between yellow, red or blue when clicking on a button. how would you code it?

PektraMom
Автор

Is there a way to make it not repeat the random numbers/strings?

raphaelchagas
Автор

first minute already helped, thanks a lot

adedimolao
Автор

I think you are from UK
try to speak slowly so we asian can get what are you saying
just a suggestion
btw nice tutorial

burhanali