在令人满意的递归函数中获得最大调用堆栈大小超出错误

时间:2015-06-03 03:27:00

标签: javascript recursion

每次调用函数时,代码都应该从数组[Non-Repeating]输出一个随机字符串,但它有时输出undefined,似乎没有模式。经过一定数量的通话后,我得到了一个超过最大呼叫堆栈大小"我认为这是来自不可满足的递归函数,但我不确定。

var Reasons = ["You're the best Cook", "You're the best Mommas", "You call me the greatest names", "You're the most Stable", "You are super funny most of the time, even though it's not on purpose sometimes", "You keep Daddy's weight in check", "You just are.", "You print money.", "You're super patient", "You're super calm alawys", "You are very quiet on the phone", "You're super up to date on all the new music", "You laugh at things I say that just aren't funny."];
var checkIfRandom = new Array();

function getRandom() {
    var randomNum = Math.floor(Math.random() * 12);
    if (checkIfRandom.indexOf(randomNum) !== -1) {
        getRandom();
        return;
    } else if (checkIfRandom.indexOf(randomNum) == -1) {
        checkIfRandom.push(randomNum);
        return (randomNum);
    }
};

function writeWord() {
    document.getElementById("reasons").innerHTML = Reasons[getRandom()] + "<br >";
};

代码在行动:http://jsfiddle.net/Luyfdcto/

0 个答案:

没有答案