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 in Project 0 now.
Once you have Node.js installed, create a directory project3
and extract the contents of this repository into the directory.
You can fetch the code quality tool (also known as a "linter"), ESLint, 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:
npm run lint
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 impact final evaluation.
For this problem, you will be implementing two interactive DatePicker
displays using a combination of HTML, CSS, and JavaScript.
In your project3
directory create a file DatePicker.js
that implements a JavaScript class named DatePicker
that can be used as in the following example:
var datePicker = new DatePicker("div1", function (id, fixedDate) {
console.log("DatePicker with id", id,
"selected date:", fixedDate.month + "/" + fixedDate.day + "/" + fixedDate.year);
});
datePicker.render(new Date("July 4, 1776"));
The constructor takes an argument consisting of the id
attribute for an existing div and a date selection callback function. When a date is selected the callback function should be called with the id
argument and an object containing the properties month
, day
, year
with the number encoding of the date (e.g. {month: 1, day: 30, year: 2016} is the encoding of January 30, 2016).
The object should have a render
method that takes one argument consisting of a Date
object that selects a particular month (the object can refer to any time within the month). When render
is invoked it replaces the contents of the date picker's div with HTML that displays a small one-month calendar such as those you might see in a travel reservation website:
Once you have created the JavaScript class, we have provided a file datepicker.html
(you do not need to modify this html file) containing two empty div elements, plus a bit of JavaScript code that invokes the DatePicker class to display a date picker in each of the divs. One of the date pickers initially displays the current month and the other displays the month of January 2009. It should be possible to change the month of each date picker independently using the controls on that date picker.
The provided html file has no styling so please create a stylesheet with the filename datepicker.css
to apply styling to the calendars and make them look nice. The corresponding link tag that requires the css file has already been added for you in the html file.
Create a file TableTemplate.js
that implements a JavaScript class named TableTemplate
with a static method fillIn
.
Requirements for the fillIn
method:
id
attribute for a <table>
, a dictionary object, and a string columnName
.{{property}}
with the corresponding property
of the dictionary object. It then fills in all {{property}}
elements in the column specified by columnName
if there exists such a column.columnName
argument is specified, the method should process the entire table.columnName
is not matched, the method should return without replacing any text in the columns. Note that you should still replace template strings in the header row regardless of whether the specified columnName
is matched.For example if you find a td
element in the matching column (assuming columnName
is specified) that has the text string "The date is the {{day}} day of month {{month}} of the year {{year}}"
and the dictionary object argument is {month: "January", day: "30", year: "2016"}
, you should update the text of the td
to be "The date is the 30 day of month January of the year 2016"
.
Calling TableTemplate.fillIn("table", dict, 'Part Number')
, where "table"
is the table on the left and dict
is a dictionary of strings, should generate the table on the right:
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 {{
). You do not need to handle nested tables or complex table cells.
You should use your template-processor.js
solution from project 2 to help you implement the fillIn
method. See the script tag ordering in the html file in the project3
directory to see how your template-processor.js
code would be loaded.
Beware that browsers insert a <tbody>
element around all of the <tr>
elements in a table, if the table doesn't already contain a <tbody>
.
Once your function has processed the entire table you should examine the visibility property of the table's style and if it is hidden update it to be visible.
Once you have created the JavaScript class, open the file test-table.html
in your browser. This file represents an HTML page containing a sample template table that that will run your code and shows what your output should look like. Do not modify this file.
parseInt
, parseFloat
, isNaN
.cells
firstChild
nextSibling
rows
tagName
textContent
innerHTML
console.log
is very useful for debugging. It takes a string argument, which it prints to the JavaScript log. You can display this log with "More Tools $\rightarrow$ Developer Tools $\rightarrow$ JavaScript console" in the Chrome control menu. If you are having trouble figuring out what is happening in your JavaScript, sprinkle console.log
statements around your code so you can see which code is being executed.debugger
statement useful in debugging. If the text "debugger;
" is executed in a JavaScript program, it causes the program to drop into the JavaScript debugger. This is analogous to a breakpoint, except that your code can control when it triggers..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.npm run lint -- --fix
.Your JavaScript must be clean (appropriate use of classes, no global variables other than constructor functions, etc.), readable, and ESLint warning-free.
Use the standard class submission mechanism to submit.
Your respository should include the following updated files.
datepicker.css
DatePicker.js
TableTemplate.js
template-processor.js
Please make sure your submission runs npm run lint and npm test with no errors or warnings.