Code-Along: Movie Watchlist
Follow these instructions to build a movie watchlist website!
Getting Started
To begin, remix 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
:function
keyword- Function name
- Parentheses
- Curly brackets
- In the body of the function (between
{
and}
), add analert
statement 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
alert
from the body of thedisplayList
function - In its place, create the basic
for
loop structure:for
- Parentheses
- Curly brackets
- For the first expression of the loop in the parentheses (init), create a variable
i
and set it to0
- For the second expression (cond), check if
i
is 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
for
loop (between{
and}
), create a new variablemovieTitle
- Set the
movieTitle
variable to be the value in themoviesToWatch
array at the current index - Under that, use an
alert
to 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
displayList
function - There, create a new variable named
listElement
- Set the value of
listElement
to be the HTML element with anid
oflist
- Under that, set the
innerHTML
of thelistElement
variable 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
for
loop (within thedisplayList
function), remove thealert
statement - In its place, create a new variable named
itemElement
- Set the value of
itemElement
to be a newly-created HTML<li>
element - Under that, set the
textContent
of theitemElement
variable to be the current movie title - Under that, append
itemElement
tolistElement
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
:function
keyword- Function name
- Parentheses
- Curly brackets
- In the body of the function (between
{
and}
), add analert
statement 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
addMovie
function, create a new variable namedmovieToAdd
- Set the value of
movieToAdd
to be the result of aprompt
statement - Set the text of the
prompt
to 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
movieToAdd
variable, still in the bodyaddMovie
- Call the
push
function on themoviesToWatch
array - Pass in
movieToAdd
as the argument for the function - Under that, call the
displayList
function 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();
}