I am making a website where you put a time of the day in, and it searches for a verse with that reference through the whole bible. For instance, if I put in 5:12, it would search through random books of the bible until it found a verse with the reference 5:12. ex: Psalms 5:12. Or at least that is how it is supposed to work. I have a loop that is supposed to find if that verse is not in a specific book, and if it is not, it goes on to the next book. That loop is not working.
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."
}
}
It works fine if "Genesis" has the reference 5:12, but if "James" doesn't have 5:12, it automatically either resorts to a "Cannot read property of undefined" error, or it outputs, "Sorry, we have no verse for that time." That should only be the output, when none of the books of the bible have that verse. ex 17:135 should output the sorry message. Most of it is working. I tried to fix it by calling a new book underneath the "i++" but that didn't fix the problem.
The website is actually online, so if you want to test it for yourselves you can go to http://calebdidthis.com/timeverse
Edit:
I made some changes, and I no longer get errors, but I do get the "sorry message" when it should be cycling through books
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."
}
}