Today we're learning how to handle lists of data, a core skill for any web developer. We'll use JavaScript Arrays to store our lists and Loops to process them, allowing us to create dynamic content from collections of data.
Almost every website or app uses lists: a list of videos on YouTube, a shopping cart, a feed of social media posts. Arrays and loops are the fundamental tools in JavaScript for working with this kind of data efficiently.
Let's break down the tools we'll be using to wrangle our lists of data.
[], with each item separated by a comma.
for loop has three parts: 1. Initialization
(let i = 0): A counter variable. 2. Condition
(i < arr.length): The loop runs as long as this is true. 3.
Increment (i++): Increase the counter after each cycle.
In the editor, you'll find an empty `generateLists()` function with step-by-step hints. Your job is to fill in the missing code to make two dynamic lists appear when you click the "Generate Lists" button!
Follow the pattern: 1. Create empty HTML string. 2. Loop through array. 3. Add HTML for each item. 4. Display on page. Copy the same pattern for both lists!
let movieHtml = '';for(let i = 0; i < movies.length; i++) {movieHtml += '<li class="list-group-item"><i class="fas fa-film"></i> ' + movies[i] + '</li>';} to close the loop, then:document.getElementById('movieList').innerHTML = movieHtml;languageHtml instead of movieHtmllanguages instead of movies'languageList' instead of 'movieList''fas fa-code' instead of 'fas fa-film'
Create a new list on the page for your favorite foods or songs. You'll need to add a new `<ul>` with an ID to the HTML, then create a new array in the JavaScript and a new `for` loop to display it.