function endGame(num) {
var total = Object.keys(abvMap).length;
$("#result").empty();
if(num == total) {
// you won
$("#result").append("<h4>YOU WON!</h4><p>You got all " + total + " states!</p>");
} else {
console.log(states);
// didn't get all 51 states
$("#result").append("<h4>ALMOST!</h4><p>You got " + num + " out of " + total +" states!</p><h3>Missing states:</h3><ul id=\"missing-states-list\"></ul>");
for(var key in abvMap) {
if(($.inArray(key, states)) == -1) {
console.log(key);
$.get("https://api.census.gov/data/2013/language?get=EST,LANLABEL,NAME&for=state:" + abvMap[key] + "&LAN=625", function(data) {
$("#missing-states-list").append("<li><div class=\"tooltip\">" + key + "<span class=\"tooltiptext\">" + data[1][0] + " spanish speakers</span></div></li>");
});
}
}
}
}
The details of this file is irrelevant. The issue here is in the for-loop, where every key
in the dictionary abvMap
is being looped through. states
is an global array that contains states that have already been found, e.g. states = ["Maryland", "Texas"]
. abvMap
is a dictionary that contains all 51 states. I'm checking in each iteration of the for-loop of the key
state have already been found. If not, I make an API call and append that state (and some data from the API) to the list #missing-states-list
.
Judging by the console.log()
outputs. There's absolutely no issue with states
or key
, it perfectly loops through every state in the dictionary. Even the API calls are correct. However, what's appended to the #missing-states-list
is always Wyoming
, which is the last entry in abvMap
. I have idea why.