Think about some different attributes of books – what do all books have? Ideas include:
In terms of the properties of books that you thought of, represent the following books as data:
You have probably had to rewrite the same kind of object multiple times at
this point; write a function makeBook that takes as arguments different
aspects of a book and returns an object representing that book that has the
proper structure (we call this a factory function).
Look at one of your book objects in the console. The object inspector is
nice, but it would be nice if we could easily view important attributes of a
book without having to click on all of its properties with the inspector.
Write a function called displayBook that takes a book as an argument, and
"pretty prints" the important parts, for example:
var sorcerersStone = { /* ... */ }
function displayBook(book) {
// ...
}
displayBook(sorcerersStone);
// => "Harry Potter and the Sorcerer's Stone, by J.K. Rowling -- fantasy, $24.99"
The output string above is only an example – the idea is that, given
a book object, displayBook returns some string that shows some subset of
the information about the book that is deemed important – what is
shown is up to you.
If you haven't already, create an array to hold all of the books that you
created above called books.
You have written the function displayBook that can be used to display a
single book as a string – write a function displayBooks that, given an
array of books, returns a single string consisting of all of the books
formatted using the displayBook function you defined before.
Each book should be numbered, and separated with a newline character so that each book is shown on a separate line in the console. The newline character is specified with a special escaped character in a string:
// Enter the below line into a console
"Hello\nWorld!"; // the \n character is a newline
function displayBooks(books) {
// ...
}
displayBooks(books);
// => "1. Harry Potter and the Sorcerer's Stone...\n2. Snow Crash, ..."
Write a function searchBooks that, given a query and an array of books,
searches the array of books for "matching" books. You'll need to make a few
decisions when implementing a search algorithm, like:
What fields will be searched? Will you search multiple fields
simultaneously (it might be best to start with one field, e.g.
title)? Should the search be case-sensitive?
How will the search work? Will it only work from the beginning of a field, or from anywhere within?
A good starting point would be to write a function isMatch that
accepts two arguments – the query and a single book –
and returns true if the book is a match, and false otherwise. Some
Hints:
"Harry Potter".toLowerCase(); // => "harry potter"
"Harry Potter".substr(0, 5); // => "Harry"
var query = "Harry";
"Harry Potter".substr(0, query.length); // => "Harry"
"Harry Potter".indexOf("Pot"); // => 6
"Harry Potter".indexOf("dog"); // => -1
Write a function removeBook that, given a book's title and an array of
books, returns a new array of books that does not contain the book with the
provided title.
As we did before, think about what kinds of aspects of movies you would like to represent. A few ideas are:
The level of granularity with some of these values is up for you to decide;
for instance, the actors/actresses could be represented with an array of
names, but what if you wanted to include the role that the actor/actress
played? Did he/she win any awards? Even the rating of a movie is open to
interpretation – is the rating from critics? Users of IMDB? Rotten
Tomatoes? Some combination?
Using the format that you decide upon, construct five movie objects.
Write a factory function for movies.
Write a function displayMovie that works like displayBook, but for
movies.
Write a function displayCast that displays the cast of a movie, including:
Create an array to hold the movies that you created called movies, and add
your movies to it.
As before, write a displayMovies function that works just like
displayBooks.
Calculate the average duration of your movies by writing a function called
averageDuration that will accept an array of movies as a parameter and
ouput the average duration. The difficulty of this problem is dependent on
how you have chosen to store the duration.
averageRating?How about searching your movies array? Write a function that works like
searchBooks, but for movies.
Tagging System: Some books have multiple genres, have won awards, are on
a best-seller list, or have other unique identifying characteristics. Let's
incorporate a tagging system that will allow us to represent all of these.
Write functions addTag and removeTag that each accept a book and a tag
as parameters, and either add tags or remove tags respectively.
Considerations:
genre key, replace it with a tag.addTag on a book that has no tags yet?addTag with the same tag (on the same book)
multiple times? Should it be possible to have the same tag twice?searchBooks to work with tags.Let's revisit your removeBooks function: what would happen if you had two
books with the same title, but different authors? Would your algorithm
remove both books? This is a common problem that we usually solve by
providing a unique identifier for each item.
removeBook to use the book's id for lookups instead of its
title.Can you think of a way to write a more abstract displayItem function that
works for books and movies (depending on how you have structured your
objects, this may or may not work well)?
Write a more general searchItems function that accepts as parameters the
query, items to search, and an array of keys that should be searched.
Refactor searchMovies and searchBooks to use this function.