函数返回undefined

时间:2012-07-23 20:16:42

标签: javascript arrays undefined

在项目上工作,我遇到了这个问题,我的数组“slicetable”每次返回undefined,我不知道如何解决这个问题。我有一个函数可以生成加密密钥长度的可能性数组 - 然后我需要将文本拆分为第n个密钥的单个字符串,直到“密钥”的长度。我已经在这个问题上工作了4个小时,不知道从哪里开始。如果有人能推荐一个好的策略来解决这样的问题,并帮助我解决这个问题,那就太棒了 - 我对这一切仍然很陌生。

function vigenerekey(text, max) {
    // Standardize the text
    text = text.split(" ").join("").toUpperCase();
    // Obtain our list of key length probabilities
    var probabilities = vigenerekeylength(text, max);
    // Extend the Math.max to arrays
    Array.max = function(array){
        return Math.max.apply(Math, array);
    };

    // Find the position of the most probable key length
    for (var d = 0; d <=12; d++) {
        if (probabilities[d] === probabilities.max) {
            return;
        }
    }
    // Slice the text into [d] parts (the length of the key)
    var slicetable = ['','','','','','','','','','','','','','','','','','','','',''];
    var chiresults = [];
    for (var e = 0; e <= d; e++){
        for (f = 0; f <= text.length; f++) {
            if (f % e === 0) {
                slicetable[e] += text.charAt(f);        
            }
        }
    return slicetable;
    }

}

2 个答案:

答案 0 :(得分:2)

你不是这个意思:(休息而不是回归)

// Find the position of the most probable key length 
for (var d = 0; d <=12; d++) { 
    if (probabilities[d] === probabilities.max) { 
        break;
    } 
} 

答案 1 :(得分:1)

你的问题是你的for循环中的return语句。你想放

break;  

而不是

return; 

我的第一个答案中有一个拼写错误(我是第一个发布答案的人),主持人删除了它。我修正了错字,但它说我无法取消删除它...所以我会再试一次。