从 javascript 中的递归函数中获取未定义
getting undefined from recursive function in javascript
我花了大约一个小时试图弄清楚为什么我从这个函数中得到未定义,这个函数运行良好,它在条件触发并且 randomAlphabet
的值正确时停止,当然是愚蠢的错误,但它让我觉得很烦人。
这是代码:
(function() {
var getAlphabet = function() {
var alphabet = [];
for ( var asciiCode = 97; asciiCode < 123; asciiCode++ ) {
alphabet.push(String.fromCharCode(asciiCode));
}
return alphabet;
};
var getRandomAlphabet = function(alphabet, randomAlphabet) {
var alphabet = alphabet || getAlphabet();
var alphabetLength = alphabet.length;
var randomAlphabet = randomAlphabet || [];
if ( alphabetLength === 0 ) {
return randomAlphabet;
};
var getRandomIndex = function(min, max) {
return Math.floor(Math.random() * (max - min) + min);
};
// remove the random item from the original alphabet
var randomIndex = getRandomIndex(0, alphabetLength);
var randomItem = alphabet.splice(randomIndex, 1)[0];
// add the random item to the random alphabet
randomAlphabet = randomAlphabet.concat(randomItem);
getRandomAlphabet(alphabet, randomAlphabet);
};
// this returns undefined
console.log(getRandomAlphabet());
})();
唯一的问题是您需要 return getRandomAlphabet
方法
(function() {
var getAlphabet = function() {
var alphabet = [];
for ( var asciiCode = 97; asciiCode < 123; asciiCode++ ) {
alphabet.push(String.fromCharCode(asciiCode));
}
return alphabet;
};
var getRandomAlphabet = function(alphabet, randomAlphabet) {
var alphabet = alphabet || getAlphabet();
var alphabetLength = alphabet.length;
var randomAlphabet = randomAlphabet || [];
if ( alphabetLength === 0 ) {
return randomAlphabet;
};
var getRandomIndex = function(min, max) {
return Math.floor(Math.random() * (max - min) + min);
};
// remove the random item from the original alphabet
var randomIndex = getRandomIndex(0, alphabetLength);
var randomItem = alphabet.splice(randomIndex, 1)[0];
// add the random item to the random alphabet
randomAlphabet = randomAlphabet.concat(randomItem);
return getRandomAlphabet(alphabet, randomAlphabet);
};
// this returns undefined
console.log(getRandomAlphabet());
})();
我花了大约一个小时试图弄清楚为什么我从这个函数中得到未定义,这个函数运行良好,它在条件触发并且 randomAlphabet
的值正确时停止,当然是愚蠢的错误,但它让我觉得很烦人。
这是代码:
(function() {
var getAlphabet = function() {
var alphabet = [];
for ( var asciiCode = 97; asciiCode < 123; asciiCode++ ) {
alphabet.push(String.fromCharCode(asciiCode));
}
return alphabet;
};
var getRandomAlphabet = function(alphabet, randomAlphabet) {
var alphabet = alphabet || getAlphabet();
var alphabetLength = alphabet.length;
var randomAlphabet = randomAlphabet || [];
if ( alphabetLength === 0 ) {
return randomAlphabet;
};
var getRandomIndex = function(min, max) {
return Math.floor(Math.random() * (max - min) + min);
};
// remove the random item from the original alphabet
var randomIndex = getRandomIndex(0, alphabetLength);
var randomItem = alphabet.splice(randomIndex, 1)[0];
// add the random item to the random alphabet
randomAlphabet = randomAlphabet.concat(randomItem);
getRandomAlphabet(alphabet, randomAlphabet);
};
// this returns undefined
console.log(getRandomAlphabet());
})();
唯一的问题是您需要 return getRandomAlphabet
方法
(function() {
var getAlphabet = function() {
var alphabet = [];
for ( var asciiCode = 97; asciiCode < 123; asciiCode++ ) {
alphabet.push(String.fromCharCode(asciiCode));
}
return alphabet;
};
var getRandomAlphabet = function(alphabet, randomAlphabet) {
var alphabet = alphabet || getAlphabet();
var alphabetLength = alphabet.length;
var randomAlphabet = randomAlphabet || [];
if ( alphabetLength === 0 ) {
return randomAlphabet;
};
var getRandomIndex = function(min, max) {
return Math.floor(Math.random() * (max - min) + min);
};
// remove the random item from the original alphabet
var randomIndex = getRandomIndex(0, alphabetLength);
var randomItem = alphabet.splice(randomIndex, 1)[0];
// add the random item to the random alphabet
randomAlphabet = randomAlphabet.concat(randomItem);
return getRandomAlphabet(alphabet, randomAlphabet);
};
// this returns undefined
console.log(getRandomAlphabet());
})();