2

I'm new to Javascript and been learning. I'm trying to set a button to activate random functions. button is fine and if I assign getGroupOne() to a button or getGroupTwo() to a button it works fine.

The only problem is somewhere in the following code.

Well when I click my button only getGroupTwo() works with the following code,

function getGroup() {
    var Group = new Array (getGroupOne(), getGroupTwo());
    var whichOne = Math.floor(Math.random()*Group.length); 
        return Group[whichOne];};

But when I flip them ... only getGroupOne() works with the following code,

function getGroup() {
    var Group = new Array (getGroupTwo(), getGroupOne());
    var whichOne = Math.floor(Math.random()*Group.length); 
        return Group[whichOne];};

Could someone let me know what is the problem, I looked at a couple answers but not completly sure how to fix this.

Thank you,

7
  • 1
    Could it be that you want to have an array of functions instead of an array of whatever these functions return? That would be var Group = [getGroupTwo, getGroupOne];. But you don't show what you are doing with the return value of getGroup, so it's hard to tell. In any case, if you want a random number from a range including the upper bound, you have to add one: Math.floor(Math.random()*(Group.length + 1)). See developer.mozilla.org/en-US/docs/JavaScript/Reference/…. Commented Aug 14, 2012 at 1:39
  • @FelixKling is probably right. You want to have var Group = [getGroupTwo, getGroupOne];', then you'll call getGroup()(). First () invokes getGroup to receive one of the two functions, the other () invokes that received function. Commented Aug 14, 2012 at 1:40
  • edit: Scratch the last part of my comment, I was somewhere else with my thoughts. Commented Aug 14, 2012 at 1:48
  • What does getGroupOne() and getGroupTwo() return? Do you want to join two arrays into var Group or do you want an array with length 2? Commented Aug 14, 2012 at 1:52
  • been playing with +1 doesn't appear to be working yet ... but will read up and play with it. I don't think it is an array, but I'm a beginner so I have functions inside of functions everywhere and trying to condense them into two groups and run them with one button. Commented Aug 14, 2012 at 1:56

1 Answer 1

1

If you want to return function references, you should write it like this:

function getGroup() 
{
    var Group = [getGroupOne, getGroupTwo],
    whichOne = Math.floor(Math.random() * Group.length);

    return Group[whichOne];
}

getGroup()();
Sign up to request clarification or add additional context in comments.

1 Comment

Ahh, that totally works was banging my head against the wall with all the other comments. thank you very much ... owe you one.

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.