Code-Along: Movie Watchlist
Follow these instructions to build a movie watchlist website!
Getting Started
To begin, copy this Starter project. It contains some HTML and CSS, but no JavaScript yet 😓
Creating the Array
The first step is to create the array that will store the list of movies.
- Open the script.js file for editing
- At the top of the file, create a new variable named
moviesToWatch - Set it equal to a new array with square brackets:
[and] - In the array, add three string values, separated by commas:
"The Lego Movie""Despicable Me""Spider-Man: Into the Spider-Verse"
Feel free to change up the films if desired! The code should end up looking something like this:
let moviesToWatch = ["The Lego Movie", "Despicable Me", "Spider-Man: Into the Spider-Verse"];
Defining the displayList Function
Now it's time to start making the buttons functional. In the index.html file, there is a <button> with an onclick set to call the displayList function... but it doesn't exist yet! It must be defined.
- Make a new line under the existing code in the script.js file
- There, define a new function named
displayList:functionkeyword- Function name
- Parentheses
- Curly brackets
- In the body of the function (between
{and}), add analertstatement for testing purposes
Run the project, and verify that clicking the "Display List" button properly executes the code in the newly defined function! It should look something like this:
function displayList() {
alert("Displaying List...");
}
Looping Through the Array
Now that the button is hooked up to a function, we have to make sure the function actually does something! The goal is for this function to display each movie in the moviesToWatch array. This will be possible with a for loop!
- Delete the
alertfrom the body of thedisplayListfunction - In its place, create the basic
forloop structure:for- Parentheses
- Curly brackets
- For the first expression of the loop in the parentheses (init), create a variable
iand set it to0 - For the second expression (cond), check if
iis less than the length of the array - For the final expression (inc), add one to the value of
i - Now, in the body of the
forloop (between{and}), create a new variablemovieTitle - Set the
movieTitlevariable to be the value in themoviesToWatcharray at the current index - Under that, use an
alertto display the currentmovieTitle
Run the project, click the "Display List" button, and verify that all movie titles are displayed! The code in the body of the displayList function should look something like this:
for (let i = 0; i < moviesToWatch.length; i++) {
let movieTitle = moviesToWatch[i];
alert(movieTitle);
}
Displaying Movies in HTML
Showing the movies in pop-ups is functional, but the user experience could definitely be better. Use some DOM manipulation to add the array items to the HTML list instead!
Getting the List Element
The first step will be to grab the HTML <ul id="list"></ul> so that we can add things to it.
- Make a new line at the top of the body of the
displayListfunction - There, create a new variable named
listElement - Set the value of
listElementto be the HTML element with anidoflist - Under that, set the
innerHTMLof thelistElementvariable to be empty
Nothing should change at this point, but storing the listElement value will make it possible to add list items for each movie! The additional JavaScript code should look something like this:
let listElement = document.querySelector("#list");
listElement.innerHTML = "";
Creating the List Items
Now that the <ul id="list"></ul> has been attained, it will be possible to add some list items! All of this will take place in the body of the for loop.
- In the body of the
forloop (within thedisplayListfunction), remove thealertstatement - In its place, create a new variable named
itemElement - Set the value of
itemElementto be a newly-created HTML<li>element - Under that, set the
textContentof theitemElementvariable to be the current movie title - Under that, append
itemElementtolistElement
Run the project, click the button, and verify that the movies are added to the page as bulleted list items! The additional code should look something like this:
let itemElement = document.createElement("li");
itemElement.textContent = movieTitle;
listElement.appendChild(itemElement);
Defining the addMovie Function
Now the basic list is all set up, but what if we want to add a new movie? There is an "Add Movie" button, but its function does not exist yet! Let's define it.
- Make a new line under the existing code in the script.js file
- There, define a new function named
addMovie:functionkeyword- Function name
- Parentheses
- Curly brackets
- In the body of the function (between
{and}), add analertstatement for testing purposes
Run the project, and verify that clicking the "Add Movie" button properly executes the code in the newly defined function! It should look something like this:
function addMovie() {
alert("Adding Movie...");
}
Getting the New Movie
Now the button has been hooked up, but the function needs to do a lot more than it's currently doing! To start, it needs to get a new movie from the user.
- In the body of the
addMoviefunction, create a new variable namedmovieToAdd - Set the value of
movieToAddto be the result of apromptstatement - Set the text of the
promptto ask "Which movie do you want to add?"
Run the project, and verify that clicking the "Add Movie" button will now prompt the user for a new movie! The added code should look something like this:
let movieToAdd = prompt("Which movie do you want to add?");
Adding the New Movie
Now we have the movie they want to add, so we can add it to the array!
- Make a new line under the
movieToAddvariable, still in the bodyaddMovie - Call the
pushfunction on themoviesToWatcharray - Pass in
movieToAddas the argument for the function - Under that, call the
displayListfunction to update the HTML
Run the project, and verify that a new movie can be added to the list on the page! The added code should look something like this:
moviesToWatch.push(movieToAdd);
displayList();
The basic functionality of the Movie Watchlist should now be working as expected!
Final Code
By the end of the activity, the JavaScript code in the script.js file should look something like this:
let moviesToWatch = ["The Lego Movie", "Despicable Me", "Spider-Man: Into the Spider-Verse"];
function displayList() {
let listElement = document.querySelector("#list");
listElement.innerHTML = "";
for (let i = 0; i < moviesToWatch.length; i++) {
let movieTitle = moviesToWatch[i];
let itemElement = document.createElement("li");
itemElement.textContent = movieTitle;
listElement.appendChild(itemElement);
}
}
function addMovie() {
let movieToAdd = prompt("Which movie do you want to add?");
moviesToWatch.push(movieToAdd);
displayList();
}