简体   繁体   English

为什么我得到这个“无法读取未定义的属性”长度”? (JavaScript)

[英]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 )? 为什么当我将它作为函数参数并以数组参数( assortment )传递时,为什么会遇到关于options.length错误?

(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]));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我收到“ TypeError:无法读取未定义的属性” length” - Why am I getting 'TypeError: Cannot read property 'length' of undefined' 谁能解释为什么我在Javascript中遇到此错误? [无法读取未定义的属性&#39;length&#39;] - Can anyone explain why I am getting this error in Javascript? [Cannot read property 'length' of undefined] 我为什么会收到TypeError:无法读取未定义的属性“现在” - Why am I getting TypeError: Cannot read property 'now' of undefined 为什么我在此 for 循环中无法读取未定义的属性“startsWith”? - Why am I getting Cannot read property 'startsWith' of undefined in this for loop? 为什么会出现“无法读取未定义的html属性”? - Why am I getting “Cannot read property of html undefined”? 为什么我越来越无法读取未定义错误的属性? - why I am getting, cannot read property of undefined error? 为什么在此示例中出现“无法读取未定义的属性&#39;文本&#39;”? - Why am I getting “Cannot read property 'text' of undefined” in this example? 为什么我无法读取 null javascript 的属性“addEventListener” - why i am getting Cannot read property 'addEventListener' of null javascript 当我可以打印未定义的变量时,为什么会出现“无法读取未定义错误的属性”? - Why Am I Getting “Cannot read property of undefined error” When I Can Print the Undefined Variable? 为什么在定义的数组上收到“ ERROR TypeError:无法读取未定义的属性&#39;length&#39;的信息”? - Why am I receiving “ERROR TypeError: Cannot read property 'length' of undefined” on a defined array?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM