function findRandomDayIndex() {
var dayindex = _.random(0, 39);
var slot = dayslots[dayindex]; // array of 40 objects
if(slot.filled === true || slot === undefined) {
return findRandomDayIndex();
} else {
return dayindex;
}
}
我收到错误:
怎样才能更好的写出函数?
最佳答案
你可以试试这个版本
function findRandomDayIndex()
{
var dayindex = Math.random(0, 39);
while( ( slot = dayslots[dayindex] ) == null )
dayindex = Math.random(0, 39);
return dayindex ;
}
请检查dayslot的一致性,以防止无限循环关于javascript - 范围错误 : Maximum call stack size error in JS function,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34466738/