8

I'm trying to run a random function but haven't quite figured it out:

<script>

function randomFrom(array) {return array[Math.floor(Math.random() * array.length)];}

function randomchords(){randomFrom(['poop()', 'poop2()', 'poop3()']);}               



function poop() { $(function() {ionian_c_vi() });  }                          

function poop2() {  $(function() {ionian_c_iii() }), $(function() {ionian_c_iv() });  }                      

function poop3() { $(function() {ionian_c_vi() }), $(function() {ionian_c_i() }), $(function() {ionian_c_ii() });  }  

</script>

and then:

<button onclick="randomchords()" >Get some random chords</button>

Am I on the right track?

4
  • 2
    What does the poop() function return? Commented May 21, 2012 at 22:28
  • Just stick some eval's in there and you'll be golden! +1 on the poop functions BTW. Commented May 21, 2012 at 22:31
  • It draws ukulele chords with Raphael! When it's done it will spit out random chord progressions. Commented May 21, 2012 at 22:39
  • What's that got to do with poop ? Commented May 21, 2012 at 22:40

4 Answers 4

9

One option is to use window object:

function randomchords() {
    var func = randomFrom(['poop', 'poop2', 'poop3']);
    window[func]();
}

Pay attention that you should remove parentheses from function names in the array.


Another option is to remove quotes from the variant above and call functions directly:

function randomchords() {
    var func = randomFrom([poop, poop2, poop3]);
    (func)();
}
Sign up to request clarification or add additional context in comments.

3 Comments

...but maybe its better to store the function in their own object, not on the global one.
@Bergi Possibly yes but the solution will be almost the same.
Why the strings? Why not just randomFrom([poop, poop2, poop3])()?
6

Functions are like values. You could say:

var myArray = [
    function(){
        ionian_c_vi();
    },
    function(){
        ionian_c_iii();
        ionian_c_iv()
    },
    function(){
        ionian_c_vi();
        ionian_c_i();
    }
];
function randomchords(){ 
     randomFrom(myArray).call();
}

For more info, look at http://www.yuiblog.com/blog/2010/02/24/video-crockonjs-3/ and/or read at https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/Call

Bookwise, read Javascript: The Good Parts (120 pages)

Helps learn JS outside jQuery :)

2 Comments

Better! But the call is still superfluous, randomFrom(myArray)() is fine.
It is true that it's superfluous, it's more like a personal choice of style - it denotes that I expect something "callable". I use call or apply always when requesting function references.
1

You're quite close:

function foo() { ... }
function bar() { ... }
function baz() { ... }

These functions are defined to the global scope, which is usually window within a browser. You can access them via string by calling window['foo'] for example. That will return the foo function itself (not the value from execution).

function randomFrom(array) {
  return array[Math.floor(Math.random() * array.length)];
}

function randomchords() {
  // add the 'window' prefix here
  randomFrom(window['foo', 'bar', 'baz'])();
}               

2 Comments

I would have upvoted this if you didn't tell that nonsense about Math.random().
D'oh, corrected my mistake after some reading. I thought Math.random() would be 0 <= x <= 1 but apparently is 0 <= x < 1 ... so forget about that being careful part :D
0

You can do this many way:

But the easiest i think to generate a random number, and call functions with case :)

$("button").click(function)
{
    var n = Math.floor(Math.random()*11);
    switch(n)
    {
        case: 0:
        Function0();
        break;

        case: 1:
        Function1();
        break;

        case: 2:
        Function2();
        break;

        ...

        case: 10:
        Function10();
        break;
}

With btn you can call

<button>Get some random chords</button>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.