Sometimes you want to pick some random number or array in JavaScript. In this tutorial, I'll show you how.
Picking a random number
1. Create a variable with any name.
2. Pick a random number using Math.random().
The above code will pick a random number from 0 - 100
3. Log the result in console.
Picking random array from variable
1. Create a variable with any name, and put your arrays in it.
2. Create a variable for picking random array and use Math.random() in it.
3. Log the results in console
Output will be random, mine was:
Final code:
Making It faster
~~
is much faster than Math.floor. So you can use it instead.
0 Comments