Description
Problem 1: Generate a Multi Filter Function
In your project2 directory, make a new le named cs142-make-multi-filter.js .The code for your Multi Filter Function will go in this le.
Declare a global function named cs142MakeMultiFilter that takes an array ( originalArray ) as a parameter and returns a function that can be used to lter the elements of this array. The returned function ( arrayFilterer ) internally keeps track of a notion called currentArray . Initially,
currentArray is set to be identical to originalArray . The arrayFilterer function takes two functions as parameters. They are:
- filterCriteria – A function that takes an array element as a parameter and returns a boolean. This function is called on every element of currentArray and currentArray is updated to re ect the results of the filterCriteria If the filterCriteria function returns false for an element, that element should be removed from currentArray . Otherwise, it is left in currentArray . If filterCriteria is not a
function, the returned function ( arrayFilterer ) should immediately return the value of currentArray with no ltering performed.
- callback – A function that will be called when the ltering is done. callback takes the value of currentArray as an argument. Accessing this inside the callback function should reference the value of originalArray . If callback is not a function, it should be ignored. callback
does not have a return value.
The arrayFilterer function should return itself unless the filterCriteria parameter is not speci ed in which case it should return the currentArray . It must be possible to have multiple arrayFilterer functions operating at the same time.
The following code shows how one might make use of the functions you de ne in this problem:
In your project2 directory, make a new le named cs142-template-processor.js . The code for your Template Processor will go in this le.
Create a template processor class ( Cs142TemplateProcessor ) that is constructed with a string parameter template and has a method fillIn . When invoked with an argument of a dictionary object, fillIn returns a string with the template lled in with values from the dictionary object. Cs142TemplateProcessor should be written using the standard JavaScript constructor and prototype structure.
The fillIn method returns the template string with any text of the form {{property}} replaced with the corresponding property of the dictionary object passed to the function.
If the template speci es a property that is not de ned in the dictionary object, the property should be replaced with an empty string. If the property is between two words, you’ll notice that replacing the property with an empty string will result in two consecutive whitespaces. Example: “This {{undefinedProperty}} is cool” -> “This is cool” . This is ne. You do not have to worry about getting rid of the extra whitespace.
Your system need only handle properly formatted properties. Its behavior can be left unde ned in the following cases as we will not be checking explicitly for them.
Problem 3: Fix cs142-test-project2.js to not pollute the global namespace
The test JavaScript le we give you ( cs142-test-project2.js ) declares numerous symbols in the global JavaScript namespace. For example, after the script is loaded the symbol p1Message appears in the global namespace. Another JavaScript le would then be able to access and change p1Message . Change cs142-test-project2.js to use the standard JavaScript module pattern using an anonymous function to hide symbols in the global namespace yet keep the same checking functionality.




