javascript辅导 | SE 261 Assignment 1: JavaScript Calisthenics

SE 261 Assignment 1: JavaScript Calisthenics
Due: Sunday, October 20, 2019 at 11:59 PM
The zip file contains the files se261-test-project2.html and se261-test-project2.js that act as a
testing framework for the code you write in this project. The JavaScript development environment is
much better in the browser, so we suggest you do your development using it.
In this project we ask you to write or modify some JavaScript functions. The problems in this
assignment are of a practical nature and functionality you develop will be useful in completing later
class projects. Given the availability of JavaScript libraries to solve or help solve pretty much most
JavaScript tasks you would be assigned, it is likely you could solve these with a couple of lines to
call some library routine.
Problem 1: Generate a Multi Filter Function (5 points)
Given a se261-make-multi-filter.js , you will see a not-complete global function named
se261MakeMultiFilter. The function se261MakeMultiFilter typically takes an array originalArray
as an argument and returns a function that can be used to filter the elements of this array.
The returned function (arrayFilterer) internally keeps track of the updated originalArray, so called
currentArray. Initially the currentArray is set to be identical to the originalArray.
Your task is to complete the returned function arrayFilterer.
• The arrayFilterer function take the following two arguments:
o cond – A function that takes an element to check whether it satisfies with a defined
condition and returns a Boolean.
o callback – A function that will be called when the filtering is done.
○ Typically, the arrayFilterer function performs checking on every element of the
current array and the current array is updated if the cond function argument returns
false for an element. This means that the element should be removed from the
current array otherwise it is left in the current array. If filter is not a function the
returned function (arrayFilterer) should immediately return the value of the current
array with no filtering performed. (Hint: In JavaScript, the convention for checking
equality/inequality is === and !==, instead of == and !=.)
• In addition, the arrayFilterer function performs checking on the callback argument. If the
callback argument is a function, then the callback function that will be called to pass the
value of current array. Accessing this inside the callback should reference the value of
originalArray. (Hint: consider to use method, refer an example of callbacks at If callback is not a function it should
be ignored. The return value of the callback is not used.
• The arrayFilterer function should return itself unless the condition argument is not
specified in which case it should return the current array. It must be possible to have multiple
arrayFilterer functions operating at the same time.
• Usage examples:
// arrayFilterer can be used to repeatedly filter this input array
var arrayFilterer = se261MakeMultiFilter([1,2,3]);
// call arrayFilterer to filter out all the numbers not equal to 2
arrayFilterer(function (elem) {
return elem !== 2; // check if element is not equal to 2
}, function (currentArray) {
console.log(this); // prints [1,2 3]
console.log(currentArray); // prints [1, 3]
// call arrayFilterer filter out all the elements not equal to 3. No callback used
arrayFilterer(function (elem) {
return elem !== 3; // check if element is not equal to 3
// call arrayFilterer with no filter should return the current array
var currentArray = arrayFilterer();
console.log(‘currentArray’, currentArray); // prints [1] since we filtered out 2
and 3
// Since arrayFilterer returns itself filters calls can be chained like:
function filterTwos(elem) { return elem !== 2; }
function filterThrees(elem) { return elem !== 3; }
var currentArray = se261MakeMultiFilter([1,2,3])(filterTwos)(filterThrees)();
console.log(‘currentArray’, currentArray); // prints [1] since we filtered out 2
and 3
// Multiple active filters at the same time
var arrayFilterer1 = se261MakeMultiFilter([1,2,3]);
var arrayFilterer2 = se261MakeMultiFilter([4,5,6]);
console.log(arrayFilterer1(filterTwos)()); // prints [1,3]
console.log(arrayFilterer2(filterThrees)()); // prints [4,5,6]
Problem 2: Template Processor (5 points)
Create a template processor function (Se261TemplateProcessor) that is constructed with a string
parameter template and has a method fillIn. Se261TemplateProcessor should be written using the
standard JavaScript constructor and prototype structure.
The fillIn method takes an argument of a dictionary object and returns a string with the template
filled in with values from the dictionary object. This means that the template string is returned with
any text of the form {{property}} replaced with the corresponding property of the dictionary object
passed to the function.
The following code gives an example of how it works:
var template = ‘My favorite month is {{month}} but not the day {{day}} or the year
var dateTemplate = new Se261TemplateProcessor(template);
var dictionary = {month: ‘July’, day: ‘1’, year: ‘2016’};
var str = dateTemplate.fillIn(dictionary);
assert(str === ‘My favorite month is July but not the day 1 or the year 2016’);
If the template specifies a property that is not defined in the dictionary object the template should be
replaced with an empty string. Your system need only handle properly formatted templates. Its
behavior can be left undefined in the case of nested templates (e.g. {{foo {{bar}}}} or unbalanced
{{ ).
HINT: If you decide to use a regular expression, try the pattern below.
this.pattern = /{{([^}]+)}}/g;
while (match = this.pattern.exec(this.result)) { … }