简体   繁体   English

计数变量未遍历for循环

[英]count variable not iterating through for loops

hey im trying to increase the count of this variable, num_pass, everytime we loop successffully through the if statements. 嘿,我每次尝试通过if语句成功循环时,都会尝试增加此变量num_pass的计数。 it keeps spitting out 0 or 1 though. 它一直吐出0或1。

i have tried placing the variable in many different places and declaring it in different places but still no success. 我曾尝试将变量放置在许多不同的地方并在不同的地方声明它,但仍然没有成功。

function pickup()
{   
var num_pass = 0;
var i;
var array = PASSENGERS[i];
for (var i = 0; i < PASSENGERS.length; i++)
{
    // get location of passengers
    var lat = PASSENGERS[i].placemark.getGeometry().getLatitude();
    var long = PASSENGERS[i].placemark.getGeometry().getLongitude();

    // calculate distance of passengers to bus
    var distance = shuttle.distance(lat, long);

    // screen for freshman

    // if passengers are close enough
    if (distance <= 15)
    {
        // if there is room on the bus
        // iterate through all the seats
        var j;
        var array = shuttle.seats;

  for (j = 0;j < shuttle.seats.length;j++)
        {

            // if a seat is empty
            if (shuttle.seats[j] == null && PASSENGERS[i].house != "Thayer Hall") 
            {          

                // remove picture from the 3-D map
                var features = earth.getFeatures();
                features.removeChild(PASSENGERS[i].placemark);

                // remove marker from the 2-D map
                PASSENGERS[i].marker.setMap(null);

                // remove 2-D map attribute of passenger
                PASSENGERS[i].marker = null;

                // add to the shuttle
                shuttle.seats[j] = PASSENGERS[i];

                // update the chart
                chart();

                $('#announcements').html("Passenger picked up!"); 
                $('#announcements').html("Score: " + score);

                num_pass++;                    


            }
            console.log(shuttle.seats[j]); 

        }


    }

    else if (num_pass > 9)
    {
        $('#announcements').html("no room on bus");
    }
    else if (distance > 15)
    {
        $('#announcements').html("no passenger nearby");
    }
}

This should work: 这应该工作:

for (j = 0; j < shuttle.seats.length; j++) {

  // if a seat is empty
  if (shuttle.seats[j] == null && PASSENGERS[i].house != "Thayer Hall") {

    // remove picture from the 3-D map
    var features = earth.getFeatures();
    features.removeChild(PASSENGERS[i].placemark);

    // remove marker from the 2-D map
    PASSENGERS[i].marker.setMap(null);

    // remove 2-D map attribute of passenger
    PASSENGERS[i].marker = null;

    // add to the shuttle
    shuttle.seats[j] = PASSENGERS[i];

    // update the chart
    chart();

    $('#announcements').html("Passenger picked up!");
    $('#announcements').html("Score: " + score);

    num_pass++;

  // @lukpaw explanation: Code below was unnecessary
  //}
  console.log(shuttle.seats[j]);

  // @lukpaw explanation: Code below was unnecessary
  //}
  }
  else if (num_pass > 9) {
    $('#announcements').html("no room on bus");
  } else if (distance > 15) {
    $('#announcements').html("no passenger nearby");
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM