1. What did you use for your nine conditions?
  2. Why?

Arrays are extremely useful for storing ordered lists of data. Think of them as the lists that most data are organized into in scripting languages.

  • Arrays are 0-indexed (start at 0)
  • values inside can be any data type
  • each value is separated by a comma
  • denoted by square brackets( [] )
  • Have access to methods, much like Strings do.

On your day4 branch, declare a var and define it with an array containing:

  • Your favorite movie of 2017
  • Your lucky number
  • Whether or not you are a Star Wars fan (using a Boolean)
  • The year you graduated high school
  • console.log the var and the length of the array.

Arrays are indexed. This means you can access the members via reference by their numbered index.

                    
                        var favArtists = ['ke$ha', 'Drake', 'Bush', 'Beck'];
                        alert(favArtists[0]);
                    
                

Great! We can manipulate data in an array. But what about more complicated arrays?

  • Nested Arrays (or arrays inside of arrays) are also possible.
  •                             
                                    
        var favoriteMovie = ['Star Wars VI', '1983'];
        var secondFavorite = ['Return of the King', '2003'];
        var thirdFavorite = ['Equilibrium', '2002'];
        var myFavoriteMovies = [];
        myFavoriteMovies.push(favoriteMovie, secondFavorite, thirdFavorite);
        //Result:
        [['Star Wars VI', '1983'], ['Return of the King', '2003'], ['Equilibrium', '2002']];
                                    
                                
                            
  • Let's get just the years of my favorite movies:
                                
    var movieYears = [];
    movieYears.push(myFavoriteMovies[0][1], myFavoriteMovies[1][1],myFavoriteMovies[2][1]);
    alert(movieYears);
                                
                            

In your scripts.js file, create 5 arrays, each with one of your top 5 movies' title and the main character's name

  1. Add all of these arrays to a final array named favoriteMovies
  2. Create an array with just the titles of your favorite movies and alert it
  3. Create another array with your favorite and least favorite of the top 5 and console.log it.
  4. Check the length of your favoriteMovies array by alerting it.

There is much more you can do with arrays. Read more about array methods here.

Get loopy! So we know how to store and manipulate data. Great! But how do we perform tasks on these data sets?

Why loops, of course!

Beware the infinite loops!

Now, using your favoriteMovies array:

  1. Write a for loop to iterate over the array
  2. Find your favorite movie by searching for the title in the loop
  3. Once the program finds your favorite movie, have it alert the title and year it was made

There is much more you can do with arrays. Read more about array methods here.

On a takehome-day4 branch of your JS repository, write a program that:

  1. Creates an array of things you need or want to do this weekend by collecting three todos from the user.
  2. Using a for loop, change the items in the array by adding a value for how many days that item should take.
  3. Hint: which data type is best suited for your todo items, based on what you will need to do to them?
  4. Using a while loop, find the task that will take the longest and log it to the console.
  5. Using a do while loop, add the string 'easy-peasy' to all the tasks other than the one that will take the longest
  6. Finally, alert all the tasks that have 'easy-peasy' in the todo array.