I have a set of defined JS functions. I want users to pick a subset of these using a comma separated string. Then I want to randomly pick from their subset, and evaluate the function selected. I have this 99% working, except it is not evaluating for some reason. Why does console keep telling me 'undefined is not a function'?

Take a peek at line 37: <http://jsfiddle.net/ftaq8q4m/>

```
// 1 User picks a subset of function names as a comma seperated string.
var effectSubset = 'func4, func5, func6';

// 2 Available functions
    function func1() {
        $('html').append('func1');
    }

    function func2() {
        $('html').append('func2');
    }

    function func3() {
        $('html').append('func3');
    }

    function func4() {
        $('html').append('func4');
    }

    function func5() {
        $('html').append('func3');
    }

    function func6() {
        $('html').append('func4');
    }

var autoPlay = function () {
    // 3 Get the user's subset of functions, turn into array
    effectArray = effectSubset.split(',');
    // 4 Pick random function from array
    var effect = effectArray[Math.floor(Math.random() * effectArray.length)];
    // 5 run the randomly picked function
    window[effect]();
}

// 6 Re-run every second
var playInterval = setInterval(autoPlay, 1000);

```
['https://Stackoverflow.com/questions/30941209', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/422203/']
I could see two things wrong:

1. Your functions were not assigned to the `window`
2. Your `"effect"` variable contained leading whitespace

I have corrected the above points here: <http://jsfiddle.net/ftaq8q4m/1/>

This appears to have resolved the issue you described.

**Example:**

```
window.func1 = function() {
    $('html').append('func1');
}

```

**And:**

```
window[effect.trim()]();

```

---

**Update**

As a bonus I fixed your misleading append messages =D 

<http://jsfiddle.net/ftaq8q4m/5/>
You approach is not working because you have a space between the function names 
`var effectSubset = 'func4, func5, func6';` After split the variable effect has a space before the name of the functions.

So the functions being called are `func4`, `<space>func5`, `<space>func6` with a space which do not exist.

Firstly, its better to have a namespace.

```
var effectSubset = 'func4,func5,func6'; // <--- remove spaces here or trim like in other answers
window.myNamespace = {};

window.myNamespace.func1 = function() {
    $('html').append('func1');
}
// ..... define other functions like above
window["myNamespace"][effect](); // <-- this should work 

```

Here is a demo **<http://jsbin.com/lixeto/edit?html,js,output>**