I have the following problem:
I want to call my functions func1()
, func2()
& func3()
in a random order. But i want to be sure that every function is called!
If it's possible it also would be nice that no functions are used; just a random order of code sequences. Like this:
function xy(){
//Call this sequence first second or third
doSomething1
//Call this sequence first second or third
doSomething2
//Call this sequence first second or third
doSomething3
//!! But call each sequence !!
}
Thanks in advance ;)
You can use something like the Fisher-Yates shuffle for shuffling the functions and then call them via Array.prototype.forEach()
:
var a = function () { alert('a'); },
b = function () { alert('b'); },
c = function () { alert('c'); },
array = [a, b, c];
array = array.map(function (a, i, o) {
var j = (Math.random() * (o.length - i) | 0) + i,
t = o[j];
o[j] = a;
return t;
});
array.forEach(function (a) { a(); });
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments