1

In pset 8, to check if a passenger is allowed to take the shuttle I must check if the passenger's house (https://shuttle.cs50.net/js/passengers.js) is in the HOUSES array (https://shuttle.cs50.net/js/houses.js).

I cannot figure out how to do that since within houses.js, house is a key and within passengers.js it is a value.

I know how to access the value of a key. How do I check the names of the keys in an array against a string value?

2 Answers 2

2

An elegant way to check if a certain variable was declared (or not) is the in operator. The check if a passenger is a freshman can be done like this:

// check for freshmen
if (!(PASSENGERS[i].house in HOUSES))
{
    $("#announcements").html("No freshmen, sorry. Blame the pset specs.");
}

PASSENGERS[i].house in HOUSES returns true if a variable of the name PASSENGERS[i].house was declared in the array HOUSES.

Thankyou Chris for steering me in the right direction.

1

if (HOUSES[PASSENGERS[0].house] == false)

Here's how:

HOUSES is a hash table underneath so when it takes in a key, I've used PASSENGERS[0].house arbitrarily, it return false if the key does not correspond to any values.

0

You must log in to answer this question.

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