首页 > 解决方案 > While 循环有时返回未定义

问题描述

我正在制作一个网站,您可以在其中输入一天中的某个时间,它会在整本圣经中搜索具有该参考的经文。例如,如果我输入 5:12,它会随机搜索圣经书籍,直到找到参考 5:12 的经文。例如:诗篇 5:12。或者至少它应该是这样工作的。我有一个循环,应该查找该诗句是否不在特定的书中,如果不是,则继续阅读下一本书。该循环不起作用。

bookList = Object.getOwnPropertyNames(esvJSON)

randBook = bookList[Object.keys(bookList)[Math.floor(Math.random() * 
66)]];
inputVerse = esvJSON[randBook][input1];

Object.size = function(obj) {
    var bookLength = 0, key;
    for (key in obj) {
        if (obj.hasOwnProperty(key)) bookLength++;
    }
    return bookLength;
};

// Get the size of an object
var bookLength = Object.size(inputVerse);

verseIndex = ["01","1", 
"2","3","4","5","6","7","8","9","10","11","12",]
var varBoolean = true;
var i = 0;;

while (varBoolean == true) {
    if (input1 > bookLength) {
        i++;
        randBook = bookList[Object.keys(bookList) . 
[Math.floor(Math.random() * 66)]];
    }
    else if (esvJSON[randBook][input1][input2] == undefined) {
        i++;
        randBook = bookList[Object.keys(bookList) . 
[Math.floor(Math.random() * 66)]];
    }
    else {
        bibleVerse = esvJSON[randBook][input1][input2];
        output = randBook + " " + input1 + ":" + input2 + " " + 
bibleVerse;
        varBoolean = false;
    }
    if (i > 66) {
        varBoolean = false;
        output = "Sorry, we have no verse for your time."
    }
}

如果“创世纪”有参考 5:12,它工作正常,但如果“詹姆斯”没有 5:12,它会自动求助于“无法读取未定义的属性”错误,或者它输出,“对不起,我们没有那个时代的诗句。” 那应该只是输出,当圣经的书都没有这节经文时。ex 17:135 应该输出抱歉消息。大部分都在工作。我试图通过在“i++”下面调用一本新书来解决它,但这并没有解决问题。

该网站实际上是在线的,所以如果您想自己测试它,可以访问http://calebdidthis.com/timeverse

编辑:

我做了一些更改,不再出现错误,但是当它应该循环浏览书籍时,我确实收到了“抱歉信息”

var varBoolean = true;
var i = 0;;

while (varBoolean == true) {
    bookList = Object.getOwnPropertyNames(esvJSON);
    randBook = bookList[Object.keys(bookList)[Math.floor(Math.random() 
* 66)]];
    inputVerse = esvJSON[randBook][input1];

Object.size = function(obj) {
    var bookLength = 0, key;
    for (key in obj) {
        if (obj.hasOwnProperty(key)) bookLength++;
    }
    return bookLength;
};

// Get the size of an object
var bookLength = Object.size(inputVerse);



if (input1 >= bookLength) {
    i++;
    //randBook = bookList[Object.keys(bookList)[Math.floor(Math.random() * 66)]];
}
else if (esvJSON[randBook][input1][input2] == undefined) {
    i++;
    //randBook = bookList[Object.keys(bookList)[Math.floor(Math.random() * 66)]];
}
else {
    bibleVerse = esvJSON[randBook][input1][input2];
    output = randBook + " " + input1 + ":" + input2 + " " + bibleVerse;
    varBoolean = false;
}
if (i > 66) {
    varBoolean = false;
    output = "Sorry, we have no verse for your time."
    }
}

标签: javascriptloopswhile-loop

解决方案


// Shuffle an array
function shuffle(a) {
    var j, x, i;
    for (i = a.length - 1; i > 0; i--) {
        j = Math.floor(Math.random() * (i + 1));
        x = a[i]; a[i] = a[j]; a[j] = x;
    }
    return a;
};

// This gives an array of keys in envJSON
bookList = Object.getOwnPropertyNames(esvJSON);

// Shuffle the array of book names as you want to go through all the books, but randomly
// This makes sure none of the books is missed while checking
var shuffledBookList = shuffle(bookList);

// Now just loop through all the books in shuffledBookList
output = null;
for (var i = 0; i < shuffledBookList.length; i++) {
    // Get the random book name from array
    var randomBookName = shuffledBookList[i];

    // Get the actual book using bookname
    randBook = esvJSON[randomBookName];

    // Get the verse at input1 from randBook
    inputVerse = randBook[input1];

    // If the verse exists and verse has a value at input2 process it and break the loop
    if (inputVerse && inputVerse[input2]) {
        bibleVerse = inputVerse[input2];
        output = randomBookName + " " + input1 + ":" + input2 + " " + bibleVerse;
        break;
    }
}

// if output is still null, show the message
if (output === null) {
    output = "Sorry, we have no verse for your time.";
}

洗牌数组逻辑取自这里


推荐阅读