CS142 Project 2 - JavaScript Calisthenics-Bai Thuc Hanh 2
CS142 Project 2 - JavaScript Calisthenics-Bai Thuc Hanh 2
Setup
Although this project has you run code in your browser, you need to have Node.js installed on your system to
run the code quality checker. If you haven't already installed Node.js and the npm package manager, follow
the installation instructions (install.html) now.
Once you have Node.js installed, create a directory project2 and extract the contents of this zip file
(downloads/project2.zip) into the directory. The zip file contains the files cs142-test-project2.html and
cs142-test-project2.js that act as a testing framework for the code you write in this project.
You can fetch the code quality tool (also known as a "linter"), ESLint (https://eslint.org/), by running the
following command in the project2 directory:
npm install
This will fetch ESLint into the node_modules subdirectory. You will be able to run it on all the JavaScript files in
project2 directory by running the command:
The code you submit should start with "use strict"; in each JavaScript file and running ESLint using the
command above should not output any errors or warnings. Any errors or warnings will be used to deduct
style points.
To run the assignment, you have two options:
1. In your browser: Open the file cs142-test-project2.html in your browser. The web page will load the
JavaScript code you have written and run a few tests.
2. In Node.js: You can also run the tests without a browser using the command
npm test
We recommend using the browser for development as the debugging environment is much better. However,
we will use Node.js for grading purposes.
Note: These tests don't cover all the edge cases. They are there to help guide you and let you know when
you have the basic functionality. It is your responsiblity to handle everything stated in the following specs
that aren't explicitly tested in the test file we give you.
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 any JavaScript tasks you would be
assigned, it is likely you could solve these with a couple of lines to call some library routine. Since the goal of
the project is to learn JavaScript, we forbid you to use any JavaScript libraries in your solutions.
Functions built-in to JavaScript, like Arrays and Date objects, are acceptable.
https://web.stanford.edu/class/cs142/project2.html 1/5
2/20/25, 2:47 PM CS142 Project 2: JavaScript Calisthenics
https://web.stanford.edu/class/cs142/project2.html 2/5
2/20/25, 2:47 PM CS142 Project 2: JavaScript Calisthenics
// Call arrayFilterer1 (with a callback function) to filter out all the numbers
// not equal to 2.
arrayFilterer1(function (elem) {
return elem !== 2; // check if element is not equal to 2
}, function (currentArray) {
// 'this' within the callback function should refer to originalArray which is [1, 2, 3]
console.log(this); // prints [1, 2, 3]
console.log(currentArray); // prints [1, 3]
});
https://web.stanford.edu/class/cs142/project2.html 3/5
2/20/25, 2:47 PM CS142 Project 2: JavaScript Calisthenics
If the template specifies a property that is not defined 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 fine. 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 undefined in the
following cases as we will not be checking explicitly for them.
nested properties - {{foo {{bar}}}} or {{{{bar}}}} or {{{bar}}}
unbalanced brackets - {{bar}}}
stray brackets in any property string - da{y or da}y
The following code shows how one might make use of the functions you define in this problem:
var template = "My favorite month is {{month}} but not the day {{day}} or the year {{year}}";
var dateTemplate = new Cs142TemplateProcessor(template);
assert(str === "My favorite month is July but not the day 1 or the year 2016");
assert(str === "My favorite month is but not the day 1 or the year 2016");
Useful Hints
In JavaScript, the convention for checking equality/inequality is === and !== , instead of == and != .
When running ESLint, make sure the .eslintrc.json (hidden file) is present, or ESLint will throw an
error. You can use the command ls -a ( dir /a in a Windows command line) to view all the files in
your current directory, including hidden files.
If ESLint output shows a lot of style issues, you can ask ESLint to fix many of them for you by running
npm run lint -- --fix .
https://web.stanford.edu/class/cs142/project2.html 4/5
2/20/25, 2:47 PM CS142 Project 2: JavaScript Calisthenics
When moving files around, make sure to do that using the command line. Dragging/dropping files
usually does not move hidden files which is a common reason for students missing their
.eslintrc.json file, which is included in the zip file you extracted at the start.
The requirement that your solution to Problem 1 support multiple simultaneous uses of
cs142MakeMultiFilter means that you should not be using global variables to store the currentArray
state.
In problem 3, you only need to address symbols created by the test file. Also, you will need to leave the
variable cs142Project2Results as a global (i.e., property on the window object) so that you can
continue to run the tests using the run-tests-using-node.js script.
Deliverables
Use the standard class submission mechanism (submit.html) to submit the project2 directory. This
directory should include the starter files we gave you along with code files you created or modified for the
problems including cs142-make-multi-filter.js , cs142-template-processor.js , cs142-test-project2.js ,
and cs142-test-project2.html . Make sure your submission runs npm run lint and npm test with no errors
or warnings.
https://web.stanford.edu/class/cs142/project2.html 5/5