Nate Nate - 23 days ago 5
Javascript Question

JavaScript Not Advancing to the Next IF Statement

I cannot figure out why my program will not move to the if statements after my first one where score=0. When I find the first city the second if statement does not fire, bringing up my alert window saying that we should find the second city. I am not sure why the if statement where score=1 is not activating.

<!DOCTYPE html>
<html>

<body>

<div id="map" style="width:100%;height:500px"></div>

<script>

var map;
var score;
score = 0;
function initMap() {
var chicago = {lat: 41.8781, lng: -87.6298};
var indianapolis = {lat: 39.7684, lng: -86.1581};
var oklahomaCity = {lat: 35.4819, lng: -97.5084};
map = new google.maps.Map(document.getElementById('map'), {
center: {lat: 0.0, lng: 0.0},
zoom: 1
});
var chicagoMarker = new google.maps.Marker({
position: chicago,


});

var oklahomaCityMarker = new google.maps.Marker({
position: oklahomaCity,
});

var indianapolisMarker = new google.maps.Marker({
position: indianapolis,

});

if (score==0) {
chicagoMarker.setMap(map);
chicagoMarker.setVisible(false);
window.alert("Find my first city!");
map.addListener('bounds_changed',function(){
zoom = map.getZoom();
console.log(zoom);
if (zoom ==2 && map.getBounds().contains(chicagoMarker.getPosition())){
window.alert("Good Start!");
}
if (zoom ==3 && map.getBounds().contains(chicagoMarker.getPosition())){
window.alert("Keep Going!");
}
if (zoom ==4 && map.getBounds().contains(chicagoMarker.getPosition())){
window.alert("Getting Closer!");
}
if (zoom ==5 && map.getBounds().contains(chicagoMarker.getPosition())){
window.alert("Closer!");
}
if (zoom ==6 && map.getBounds().contains(chicagoMarker.getPosition())){
window.alert("Keep Going!");
}
if (zoom ==7 && map.getBounds().contains(chicagoMarker.getPosition())){
window.alert("Almost There!");
}
if (zoom ==8 && map.getBounds().contains(chicagoMarker.getPosition())){
window.alert("You found Chicago! Your Score is: 1");
score = 1;
}
})
};


if (score==1) {
oklahomaCityMarker.setMap(map)
oklahomaCityMarker.setVisible(false);
window.alert("Find my second city!");
map.addListener('bounds_changed',function(){
zoom = map.getZoom();
console.log(zoom);
if (zoom ==2 && map.getBounds().contains(oklahomaCityMarker.getPosition())){
window.alert("Good Start!");
}
if (zoom ==3 && map.getBounds().contains(oklahomaCityMarker.getPosition())){
window.alert("Keep Going!");
}
if (zoom ==4 && map.getBounds().contains(oklahomaCityMarker.getPosition())){
window.alert("Getting Closer!");
}
if (zoom ==5 && map.getBounds().contains(oklahomaCityMarker.getPosition())){
window.alert("Closer!");
}
if (zoom ==6 && map.getBounds().contains(oklahomaCityMarker.getPosition())){
window.alert("Keep Going!");
}
if (zoom ==7 && map.getBounds().contains(oklahomaCityMarker.getPosition())){
window.alert("Almost There!");
}
if (zoom ==8 && map.getBounds().contains(oklahomaCityMarker.getPosition())){
window.alert("You found Chicago! Your Score is: 1");
google.maps.event.ClearListeners('bounds_changed');
}
})
score = score + 1;
google.maps.event.ClearListeners('bounds_changed');

};

if (score==2) {
indianapolisMarker.setMap(map)
indianapolisMarker.setVisible(false);
window.alert("Find my first city!");
map.addListener('bounds_changed',function(){
zoom = map.getZoom();
console.log(zoom);
if (zoom ==2 && map.getBounds().contains(indianapolisMarker.getPosition())){
window.alert("Good Start!");
}
if (zoom ==3 && map.getBounds().contains(indianapolisMarker.getPosition())){
window.alert("Keep Going!");
}
if (zoom ==4 && map.getBounds().contains(indianapolisMarker.getPosition())){
window.alert("Getting Closer!");
}
if (zoom ==5 && map.getBounds().contains(indianapolisMarker.getPosition())){
window.alert("Closer!");
}
if (zoom ==6 && map.getBounds().contains(indianapolisMarker.getPosition())){
window.alert("Keep Going!");
}
if (zoom ==7 && map.getBounds().contains(indianapolisMarker.getPosition())){
window.alert("Almost There!");
}
if (zoom ==8 && map.getBounds().contains(indianapolisMarker.getPosition())){
window.alert("You found Chicago! Your Score is: 2");
}
})
score = score + 1;
google.maps.event.ClearListeners('bounds_changed');

};
}
</script>
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCuvsCAF0gVmwv6AF0SoA3xBjV66RG4r7o&callback=initMap"
async defer></script>
</body>
</html>

Answer

It doesn't move to the next if because the window.alert statement freezes the program execution until you dismiss it. Consider using a non-blocking popup component, like jQuery-ui dialog or Bootstrap-ui modals for example.

Comments