0

Granted it's the end of a long week and my brain's fried, but I'm at my wit's end with this. Why on earth am I getting this error with respect to options.length when it's a function parameter and it was passed in an array argument (assortment)?

(I'm trying to come up with a recursive solution to provide permutations (repetitions allowed) of an assortment of characters in an array)

var n = 10;
var assortment = ['a','b','c','d','e'];
console.log(permutations(n, assortment));

function permutations (num, options) {
    var solutions = [];
    var singleSet = [];
    if (singleSet.length === num) {
        solutions.push(singleSet);
        return;
    } else {
        for (var i=0; i < options.length; i++) {
            singleSet = singleSet.concat(permutations(options[i]));
        }
    }
    return solutions;
}
1
  • Where is the part where permutations (num, options) is called? Commented May 24, 2015 at 1:40

1 Answer 1

3

You only pass one argument here:

    singleSet = singleSet.concat(permutations(options[i]));
Sign up to request clarification or add additional context in comments.

3 Comments

You're right and thank you. I added something (num) for the first argument and that error disappeared. Still puzzled why the error was registering for options.length in the for loop instead.
That is because for Javascript functions you cannot make a fixed definition as of how many parameters have to be passed to it.
Because that's the first time you reference options. And options is undefined...cause you never passed it in :)

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.