简体   繁体   中英

Why am I getting this 'cannot read property 'length' of undefined'? (JavaScript)

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

您仅在此处传递一个参数:

    singleSet = singleSet.concat(permutations(options[i]));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM