Javascript 获取最大调用堆栈大小超出可满足递归函数中的错误

Javascript 获取最大调用堆栈大小超出可满足递归函数中的错误,javascript,recursion,Javascript,Recursion,每次调用函数时,代码都应该从数组[非重复]中输出一个随机字符串,但有时它会输出未定义的字符串,并且似乎没有模式。经过一定数量的调用后,我得到了一个“超过最大调用堆栈大小”,我认为这是一个不可满足的递归函数,但我不确定 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 f

每次调用函数时,代码都应该从数组[非重复]中输出一个随机字符串,但有时它会输出未定义的字符串,并且似乎没有模式。经过一定数量的调用后,我得到了一个“超过最大调用堆栈大小”,我认为这是一个不可满足的递归函数,但我不确定

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 >";
};
var Reasons=[“你是最好的厨师”,“你是最好的妈妈”,“你叫我最伟大的名字”,“你是最稳定的”,“你大多数时候都超级有趣,即使有时不是故意的”,“你控制着爸爸的体重”,“你就是。”,“你印钞”,“你超级耐心”,“你超级冷静”,“你在电话里非常安静”,“你对所有的新音乐都非常了解”,“你嘲笑我说的那些不好笑的话。”];
var checkIfRandom=新数组();
函数getRandom(){
var randomNum=Math.floor(Math.random()*12);
if(checkIfRandom.indexOf(randomNum)!=-1){
getRandom();
返回;
}else if(checkIfRandom.indexOf(randomNum)=-1){
checkIfRandom.push(randomNum);
返回(randomNum);
}
};
函数writeWord(){
document.getElementById(“原因”).innerHTML=原因[getRandom()]+“
”; };

动作中的代码:

可能您调用了writeWord超过12次,但它没有找到未使用的索引?好吧,返回;确实返回未定义的。可能您想返回递归调用的结果?原因[getRandom()]的预期结果是什么?而不是getRandom();return;,为什么不使用
return getRandom();