How can I make a function that happen depends on chances...... | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

How can I make a function that happen depends on chances......

how can I make a function that will only happens with a chance of 50 %

21st Mar 2017, 2:11 AM
Leon lit
Leon lit - avatar
1 Answer
+ 14
Use Math.floor() and Math.random() to generate a 0 or 1, and if it's 0, run the code. var num = Math.floor(Math.random() * 2) if (num === 0) { //your code } //else nothing happens when the function is called
21st Mar 2017, 3:23 AM
Tamra
Tamra - avatar