Module 5 OH Welcome & Objectives

Exercise 1: Setup

const movies = ["Interstellar", "Inception", "The Matrix", "Arrival", "The Prestige"];

.push() and .pop()

Exercise: Add two movies, then remove the last and log it.

.shift() and .unshift()

Exercise: Add to the start, remove the first movie, and print the updated array.

.splice()

.splice(index, numberToRemove, item1, item2, ...)

Exercise: Remove the 2nd movie. Insert a new movie at position 2.

.slice()

.slice(startIndex, endIndex) → returns new array

Exercise: Create a new array with the first 3 movies using .slice().

.indexOf() and .includes()

Exercise: Check if "The Matrix" is in your array. Print index of "Inception".

.join()

.join(separator) → makes string from array

Exercise: Create a string of movie titles separated by " | ".

.forEach()

Run function for each item

Exercise: Print "One of my favorite movies is: <movie>" for each item.

.map()

Create a new modified array

Exercise: Create a new array of uppercase movie names.

.filter()

Returns items that match a condition

Exercise: Create a new array of movie titles longer than 8 characters.

.reduce()

Condense array into one value

Exercise: Calculate total number of characters across all movie titles.

Array Challenge 🏆

Create a function favoriteMovieString(arr) that:

  1. Takes in an array of movie titles
  2. Returns a string like: "I love these movies: Interstellar, Inception, The Matrix."

Wrap Up

Strings and String Manipulation

Exercise: Write a function that capitalizes the first letter of a given string.

String Template Literals

Syntax: `Hello, ${name}!`

Exercise: Write a function that returns: `My favorite movie is ${movie}`

Anonymous Functions & Fat Arrows

Example: const greet = (name) => `Hi, ${name}`;

Exercise: Write an anonymous arrow function that squares a number.

Function and Block Scope

Exercise: Create a function that declares a let variable inside and console.logs it.

Passing Functions by Reference

Exercise: Use .bind() to bind a function to an object and call it.

ES6 Modules

Exercise: Write two small files: one that exports a function and one that imports and calls it.

Final Demo: "Build a Movie Database" 🎬

Combine what we've learned to build a small interactive app:

Stretch Goal: Add a feature to capitalize all movie titles.

Setting Up a Local Server

To run ES6 modules in the browser, we need a local server:

  1. Install http-server globally:
    npx http-server
  2. Navigate to your project directory in terminal
  3. Start the server:
    http-server
  4. Open your browser and go to:
    http://localhost:8080
Note: The server automatically serves index.html when you visit the root URL (http://localhost:8080). You don't need to specify index.html in the URL.