JavaScript random order functions

Sebastian Schneider

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 ;)

Nina Scholz

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Javascript slideshow in random order

From Dev

Javascript functions parsing order

From Dev

Higher order javascript functions

From Dev

Understanding Higher Order functions in Javascript

From Dev

Higher-order functions in Javascript

From Dev

Javascript functions not calling in correct order

From Dev

Buttons disabling in a random order, Rails/javascript

From Dev

javascript - display in a random order everytime page is refreshed

From Dev

Order of execution of functions bound to an event in Javascript

From Dev

Do javascript functions need to be declared in the reverse order?

From Dev

Javascript Object Cloning in Higher Order Functions

From Dev

javascript functions calls in order, made by JSP

From Dev

Javascript Object Cloning in Higher Order Functions

From Dev

JavaScript functions not executed in the right order with sleep

From Dev

for() and random() FUNCTIONS

From Dev

Javascript quiz - display choices in a random order using Array.prototype

From Dev

Trouble understanding a basic concept in Javascript Higher Order Functions

From Dev

ORDER BY random() with seed in SQLITE

From Java

Sorting an Array in Random Order

From Dev

Order by random in RethinkDB

From Dev

Doing tasks in a random order

From Dev

sqlite order by random groups

From Dev

Print array in random order?

From Dev

Change the order of random sentences

From Dev

How to order by random() in ORMLite

From Dev

ORDER BY RANDOM - With or without replacement?

From Dev

Mysql order by and random not working

From Dev

ORDER BY RANDOM() in Cordova/Phonegap

From Dev

Reorder columns in a random order