Share
Explore

JS Functions Knowledge Test

Knowledge Testing Summary Activity

For today do only Exercise 4:
Get the code running in VSC.
Put a screen shot of the working code into a Word Document.
Name word Document as StudentName_StudentID.docx
To ensure students have grasped the concepts discussed, let's create a summary activity where they must demonstrate their understanding of higher-order functions, constructor functions, arrow functions, generator functions, and immediately invoked function expressions (IIFEs).
This activity will test their ability to apply these concepts in a practical scenario.

Activity Instructions

Objective: Implement a mini library management system using the concepts discussed. This system will involve managing books and authors, enrolling students in classes, and generating reports.

Part 1: Constructor Functions and Prototype

Define Constructor Functions:
Create a constructor function for Author.
Create a constructor function for Book.
Prototype Methods:
Add a method to the Author prototype to return a short biography.
Add a method to the Book prototype to return a summary of the book.

Part 2: Higher-Order Functions

Array of Books:
Create an array of Book objects.
Filter Books:
Implement a higher-order function to filter books published after a certain year.

Part 3: Arrow Functions and IIFEs

Arrow Function:
Use an arrow function to list the titles of all books.
IIFE:
Create an IIFE to initialize the system with some authors and books and log the initial setup.

Part 4: Generator Functions

Generator Function:
Implement a generator function to yield books one by one.

Example Code Structure

Below is a template for students to complete the activity:
// Part 1: Constructor Functions and Prototype

// Constructor function for Author
function Author(name, birthYear) {
this.name = name;
this.birthYear = birthYear;
}

// Add a method to the Author prototype
Author.prototype.getBio = function() {
return `${this.name} was born in ${this.birthYear}.`;
};

// Constructor function for Book
function Book(title, author, publicationYear) {
this.title = title;
this.author = author; // author should be an instance of Author
this.publicationYear = publicationYear;
}

// Add a method to the Book prototype
Book.prototype.getSummary = function() {
return `${this.title}, written by ${this.author.name}, was published in ${this.publicationYear}.`;
};

// Part 2: Higher-Order Functions

const books = [
new Book("Harry Potter and the Philosopher's Stone", new Author("J.K. Rowling", 1965), 1997),
new Book("1984", new Author("George Orwell", 1903), 1949),
new Book("The Hobbit", new Author("J.R.R. Tolkien", 1892), 1937),
new Book("The Catcher in the Rye", new Author("J.D. Salinger", 1919), 1951)
];

// Higher-order function to filter books by year
function filterBooksByYear(books, year) {
return books.filter(book => book.publicationYear > year);
}

// Part 3: Arrow Functions and IIFEs

// Arrow function to list book titles
const listBookTitles = (books) => books.map(book => book.title);

// IIFE to initialize the system
(function() {
console.log("Initializing Library System...");
books.forEach(book => {
console.log(book.getSummary());
});
})();

// Part 4: Generator Functions

// Generator function to yield books one by one
function* bookGenerator(books) {
for (const book of books) {
yield book;
}
}

// Testing the generator function
const bookGen = bookGenerator(books);
let nextBook = bookGen.next();
while (!nextBook.done) {
console.log(nextBook.value.getSummary());
nextBook = bookGen.next();
}

// Knowledge Testing Summary

console.log("Author Bio:");
console.log(new Author("J.K. Rowling", 1965).getBio()); // J.K. Rowling was born in 1965.

console.log("Filtered Books (published after 1950):");
const filteredBooks = filterBooksByYear(books, 1950);
filteredBooks.forEach(book => console.log(book.getSummary()));

console.log("List of Book Titles:");
console.log(listBookTitles(books));

Submission Requirements

Constructor Functions and Prototypes: Students should define the Author and Book constructor functions and add appropriate methods to their prototypes.
Higher-Order Functions: Implement the filterBooksByYear function to filter books by their publication year.
Arrow Functions and IIFEs: Use an arrow function to list the book titles and an IIFE to initialize and log the system setup.
Generator Functions: Create a generator function to yield books and test it by logging the summaries of the books one by one.

Evaluation Criteria

Correctness: The code should run without errors and produce the expected output.
Use of Concepts: Students should correctly apply constructor functions, prototypes, higher-order functions, arrow functions, IIFEs, and generator functions as described.
Code Quality: The code should be well-organized, readable, and follow best practices.
This activity will help students demonstrate their understanding of the key concepts discussed and how to apply them in practical scenarios.
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.