Assignments > Tutorial 6: JavaScript: Practice with higher-order iteration functions
Due on Mon, 03/03 @ 11:59PM. 6 Points.
For Tutorial 6, you will make a UNCA Course Search interface for the Computer Science Department that works like this:
Please download the starter files below and then complete the following tasks:
I. Implement the helper functions
1. Filter functions
Implement two filter functions (which should return either true or false):
isClassFull
: This function will take a course object as an argument and returntrue
if the course is full, andfalse
if it is not.doesTermMatch
: This function will take a course object as an argument and returntrue
if the course “matches” the search term, andfalse
if it does not.- Use your discretion to determine a good matching algorithm. For instance, you could return
true
if the search string matches (or partially matches) one or more of the data fields (Code, CRN, Title, one of the instructor’s names, etc.).
- Use your discretion to determine a good matching algorithm. For instance, you could return
// Part 1.1a
const isClassFull = course => {
// modify this
return true;
}
// Part 1.1b
const doesTermMatch = course => {
// modify this
return true;
}
Tips
Use some of the JavaScript built-in string methods. It also might be useful to convert everything to uppercase / lowercase. Some particularly useful methods to checkout:
- includes()
- toUpperCase()
- toLowerCase()
2. “Data to HTML” function
Implement the dataToHTML
function, which takes a course object as an argument and returns an HTML string that represents the course.
- See the
index.html
file to examine the structure of the HTML “card” that represents a course.
// Part 1.2
const dataToHTML = (course) => {
// modify this to be more detailed
return `
<section class="course">
${course.Code}
</section>
`;
};
Tips
- Use a template literal (backticks).
- Feel free to create some helper variables to format the string output.
II. Implement the showMatchingCourses function / event handler
Implement the showMatchingCourses function
To actually display relevant course “cards” to the screen, you will also need to implement a showMatchingCourses
function. To do this, use the built-in filter and forEach higher-order array methods – and any relevant DOM methods – to build the interface. Specifically, you will:
- Clear out the existing courses in the DOM.
- Use the array’s built in “filter” method, which takes a filter function as an argument, to return an array of objects that match the search criteria.
- You will make use of the
isClassFull
anddoesTermMatch
functions. - Consider chaining multiple invocations of the filter method to progressively winnow down the courses matching the search criteria.
For instance:const matches = courseList.filter(isClassFull).filter(doesTermMatch)
).
- You will make use of the
- Use the array’s built in “forEach” method to:
- Generate an HTML snippet of the course (by invoking the
dataToHTML
function), and - Insert the HTML snippet into the DOM (suggestion: use the
insertAdjacentHTML
method).
- Generate an HTML snippet of the course (by invoking the
// Part 2
const showMatchingCourses = () => {
console.log(`Search term: ${searchTerm}`);
console.log(`Only show open classes: ${openOnly}`);
console.log(`Course data: ${courseList}`);
// output all of the matching courses to the screen:
};
III. What to Submit
When you’re done, please create a link from your homepage to your Tutorial 6 web page (see Sarah’s homepage for an example). Then, commit and push all of your edits to GitHub and, paste a link to your GitHub Repository and to your GitHub pages in the Moodle submission.
If you collaborated with someone, please list your partner’s name in the comments section.