Skip to content

Instantly share code, notes, and snippets.

@sipple
Created July 22, 2014 15:18
Show Gist options
  • Save sipple/0720c8931591965c83ed to your computer and use it in GitHub Desktop.
Save sipple/0720c8931591965c83ed to your computer and use it in GitHub Desktop.

Relieved of Duty!

Okay…ready for a pretty tough one?

The ranger-devs have really had a lot of success with your vehicle objects. They’ve even created their own objects that represent each ranger-dev and placed those objects inside your vehicle objects to keep track of which ranger-devs are assigned to patrol duty on specific vehicles. For example, the modified submarine object is as follows:

var vehicle3 = {
  type: "Submarine", capacity: 8, storedAt: "Underwater Outpost",
  ranger1: { name: "Gregg Pollack", skillz: "Lasering", dayOff: "Friday"},
  ranger2: { name: "Bijan Boustani", skillz: "Roundhouse Kicks", dayOff: "Tuesday"},
  ranger3: { name: "Ashley Smith", skillz: "Torpedoing", dayOff: "Friday"},
  ranger4: { name: "Mark Krupinski", skillz: "Sniping", dayOff: "Wednesday"},
  numRangers: 4
};

Alas, however, they haven’t yet built a function that relieves ranger-devs from duty because the difficulty is just TOO DANG HARD! Removing any rangers means that the remaining rangers would need to be renumbered. They’ve asked for your assistance in getting the off-duty rangers OUT of the vehicle while holding on to their objects for further use, as well as re-numbering the rangers who should remain on duty IN the vehicle. Phew!

Build a declared function, called relieveDuty, that accepts a vehicle object, vehicle, and a day of the week, day. This function should do the following:

Search only over all of the ranger objects contained within the vehicle object, creatively using the existing numRangers property to do so. No need to look at the other properties in the vehicle.

Every ranger’s dayOff should be checked. If the dayOff matches the passed in day, the ranger’s object should be added to an array of objects, called offDuty, which will eventually be returned. If the dayOff does NOT match the day, add the ranger object to an array called onDuty.

In both cases, the existing ranger object should be removed from the vehicle, either because it’s property name will be renumbered OR because the ranger is off-duty.

The numRangers property should be adjusted correctly for the amount of rangers still on duty.

The rangers staying in the vehicle should be re-added to the vehicle, with property names rebuilt using the onDuty array, in the order that the rangers were added to that array. Their new ranger numbers should start with 1, as in the example above.

Finally, to test your might and consider you for a promotion, the rangers would like you to build the entire function using only bracket notation…no dot notation allowed on regular object properties. Any array methods you might use, however, are okay to use with dots as normal.

Then, after building the function, call it on vehicle3 and "Friday". The resulting array of objects should be stored in a variable called offToday.

Use the hints as you get stuck, and by all means, work on it in your own console if you want to experiment with some objects.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment