0

For dropoff() in my pset8 code I'm stuck at the following line

//find house in range
  for (var i in HOUSES)  

      if in range

          // iterate over seats
          for (var j in shuttle.seats)

 ---->        if (shuttle.seats[j].house == HOUSES[i])       <-----

Here I get stuck...

I found the house that is in range, by iterating over the HOUSES array and check if HOUSES[i] is in the 30 metres range.

If so, I should compare the .house value of each seated passenger to the house in the HOUSES array that is in range.

How do I do that??

HOUSES[i] returns the whole structure (.lat and .lng)

I want to compare the shuttle.seats[j].house value to the name/key of HOUSES[i]...

Is there a way?

1 Answer 1

1

You can still access the lat and lng of a house using something like

HOUSES["Adams House"].lat; // access the lat
HOUSES["Adams House"].lng; // access the lng

and use these along with a method like distance (defined in shuttle.js) to calculate the distance between the shuttle and the house.


Update: I'm not sure why you care about the name of the house while all you're asked to do is to check whether the house of the current passenger is in the specified range and you could achieve this with something as simple as this

var passengerHouse = the house of the current passenger
var distance = shuttle.distance(passengerHouse.lat, passengerHouse.lng)

if distance is in the specified range
    drop off

If you rather want to access the property names (the house names in this case), you could probably use a for ... in loop as follows

var obj = {
    "a" : 1,
    "b" : 2,
    "c" : 3
};

for (var prop in obj)
    console.log(prop);

Output:

a
b
c
4
  • This is indeed a different method.. One that works, thanks :)
    – Jesse
    Commented Oct 24, 2014 at 18:02
  • @JesseLisser if you have any further question, you may still update your original question! If this answers your question, you may please accept the answer to mark your question as solved! Thank you!
    – kzidane
    Commented Oct 24, 2014 at 18:50
  • Thanks! I did update the question, as I'm still curious if my idea could work or not....
    – Jesse
    Commented Oct 27, 2014 at 19:41
  • @JesseLisser I've just updated my answer!
    – kzidane
    Commented Oct 27, 2014 at 21:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .