Attempting to complete what I initially thought was a simple task has led me to believe that I may have oversimplified the process or made a mistake in my loop. My objective is to browse through a series of links containing JSON objects in order to identify specific ones with particular data.
The JSON links under review have the following format:
Link 1:
{"genre": "fantasy", "title": "Book 1"}
Link 2:
{"genre": "scifi", "title": "Book 2"}
The aim of my loop is to locate a book categorized as 'scifi', however, my current code does not seem to produce any results. If anyone can pinpoint where I may have erred, your assistance would be greatly appreciated. As it may not be evident, I am relatively new to this, so I welcome any suggestions for improvement.
function find_book(){ // triggered by button onclick
var i = curr_line+1 // initial value for current line is 0
var x = 0;
var limit = bookcount-1; // total number of books stored in bookcount variable
while(x != 1) {
request= new XMLHttpRequest();
request.open("GET","http://example.com/books/?book="+i,true);
request.onreadystatechange=function() {
if(request.readyState==4 && request.status==200) {
var jsonbook = JSON.parse(request.responseText);
alert('genre is'+jsonbook.genre);
}
}
request.send()
if(jsonbook.genre == "scifi"){
alert('book is at '+i);
x=1;
}
if(i>limit){
alert('end of book list');
x=1;
}
i++;
}
}