IRM4724
Assignment 4
2024
, Question 1: Write a JavaScript function to add the following books in the
library collection for author John Grisham (30 Marks)
To begin, we'll create a JavaScript function to add books to the library collection. We will use an
array to store the books, where each book is represented by an object containing the title, author,
and year of publication.
```javascript
// Initialize the library collection as an empty array
let libraryCollection = [];
// Function to add a book to the library collection
function addBook(title, author, year) {
libraryCollection.push({ title: title, author: author, year:
year });
}
// Add books by John Grisham to the library collection
addBook("A Time to Kill", "John Grisham", 1989);
addBook("The Firm", "John Grisham", 1991);
addBook("The Pelican Brief", "John Grisham", 1992);
addBook("The Client", "John Grisham", 1993);
addBook("The Chamber", "John Grisham", 1994);
addBook("The Rainmaker", "John Grisham", 1995);
addBook("The Runaway Jury", "John Grisham", 1996);
addBook("The Partner", "John Grisham", 1997);
addBook("The Street Lawyer", "John Grisham", 1998);
addBook("The Testament", "John Grisham", 1999);
addBook("The Brethren", "John Grisham", 2000);
addBook("A Painted House", "John Grisham", 2001);
addBook("Skipping Christmas", "John Grisham", 2001);
addBook("The Summons", "John Grisham", 2002);
addBook("The King of Torts", "John Grisham", 2003);
addBook("Bleachers", "John Grisham", 2003);
addBook("The Last Juror", "John Grisham", 2004);
addBook("The Broker", "John Grisham", 2005);
addBook("Playing for Pizza", "John Grisham", 2007);
addBook("The Appeal", "John Grisham", 2008);
addBook("The Associate", "John Grisham", 2009);
addBook("The Confession", "John Grisham", 2010);
addBook("The Litigators", "John Grisham", 2011);
addBook("Calico Joe", "John Grisham", 2012);
addBook("The Racketeer", "John Grisham", 2012);
addBook("Sycamore Row", "John Grisham", 2013);
addBook("Gray Mountain", "John Grisham", 2014);
console.log(libraryCollection);
```