FE-102

Modified on Fri, 7 Jun at 12:41 PM

Need help in the implementation of filterFunctions(), filterByDuration(), and filterByCategory()? (ID: FE21)


The filterFunction() takes in:

  1. adventures:  array of objects (all the available adventures) 

  2. filters : object having 2 properties.: duration (string) and category (array of strings)

As we can see in index.html, the filters object is of the following format:


Suppose the user of your app has selected Cycling and Beaches categories, and has chosen duration of 2-4, then filters would be as follows:

{ duration: "2-4", category: ["Cycling", "Beaches"] }

And, filterFunction() should return an array of objects (only those adventures whose category matches with any one of the categories in the category array of the filters object, and whose start time and end time matches with the start time and end time mentioned in the duration string of the filters object).

When filterFunction() is called, depending upon the choices made by the user, the filters object can be one among the four types:

Note that filterByDuration() does not take the duration as a string but takes it in as 2 numbers, the low-time in the duration range and high-time mentioned in the duration range. You can extract these numbers from the filters.duration string by splitting it on the basis of the - (hyphen) delimiter. Hint: use .split(“-”) on filters.duration


While implementing the filterFunction(), think of filterByDuration() as a black box that will accept adventures' list, low-time, high-time and return the adventures whose durations lie in those ranges, and think of filterByCategory() as a black box which will accept adventures’ list and category array and will return only those adventures which will have one among those categories.

Pseudo-code for filterFunction():

filteredAdventures = [ ]

If filters object has both, duration as well as category:

    Extract low-time and high-time from filters.duration.

    filteredAsPerDuration = filterByDuration(list, low-time, high-time);

    filteredAdventure = filterByCategory(filteredAsPerDuration, filters);

Else if filters object has only duration:

    Extract low-time and high-time from filters.duration.

    Call filterByDuration and store in filteredAdventures what it returns

Else if filters object has only category:

    Call filterByCategory and store in filteredAdventures what it returns

Else (i.e. no filters selected, so no need to do filtering)

    filteredAdventures = list;

Return filteredAdventures 

    

Approach for filterByDuration():

Iterate over the given list of adventures (array of objects), and for each of the adventure check if its low time is >= the low time parameter and its high time is <= the high time parameter. 

If yes, push this adventure in the array containing adventures filtered based on duration.

Tip: Use filter() method for convenience.


Approach for filterByCategory():

Iterate over the given list of adventures (array of objects), and for each of the adventure check if its category is included in the categoryList array. If yes, push this adventure in the array containing adventures filtered based on category.

Tip: Use filter() method for convenience.

Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select at least one of the reasons
CAPTCHA verification is required.

Feedback sent

We appreciate your effort and will try to fix the article