Get Programming with JavaScript - Listing 4.02
Listing 4.02 - Displaying information from similar objects
JS Bin
var movie1;
var movie2;
var movie3;
movie1 = {
title: "Inside Out",
actors: "Amy Poehler, Bill Hader",
directors: "Pete Doctor, Ronaldo Del Carmen"
};
movie2 = {
title: "Spectre",
actors: "Daniel Craig, Christoph Waltz",
directors: "Sam Mendes"
};
movie3 = {
title: "Star Wars: Episode VII - The Force Awakens",
actors: "Harrison Ford, Mark Hamill, Carrie Fisher",
directors: "J.J.Abrams"
};
console.log("Movie information for " + movie1.title);
console.log("------------------------------");
console.log("Actors: " + movie1.actors);
console.log("Directors: " + movie1.directors);
console.log("------------------------------");
console.log("Movie information for " + movie2.title);
console.log("------------------------------");
console.log("Actors: " + movie2.actors);
console.log("Directors: " + movie2.directors);
console.log("------------------------------");
console.log("Movie information for " + movie3.title);
console.log("------------------------------");
console.log("Actors: " + movie3.actors);
console.log("Directors: " + movie3.directors);
console.log("------------------------------");
Further Adventures
Listing 4.02 - Displaying information from similar objects - Task 1
- Add a fourth movie and display its info.
var movie1;
var movie2;
var movie3;
var movie4; // Declare a fourth variable
movie1 = {
title: "Inside Out",
actors: "Amy Poehler, Bill Hader",
directors: "Pete Doctor, Ronaldo Del Carmen"
};
movie2 = {
title: "Spectre",
actors: "Daniel Craig, Christoph Waltz",
directors: "Sam Mendes"
};
movie3 = {
title: "Star Wars: Episode VII - The Force Awakens",
actors: "Harrison Ford, Mark Hamill, Carrie Fisher",
directors: "J.J.Abrams"
};
// Create a fourth movie object
// Assign it to the fourth variable
movie4 = {
title: "Tomorrowland",
actors: "George Clooney, Britt Robertson
directors: "Brad Bird"
};
console.log("Movie information for " + movie1.title);
console.log("------------------------------");
console.log("Actors: " + movie1.actors);
console.log("Directors: " + movie1.directors);
console.log("------------------------------");
console.log("Movie information for " + movie2.title);
console.log("------------------------------");
console.log("Actors: " + movie2.actors);
console.log("Directors: " + movie2.directors);
console.log("------------------------------");
console.log("Movie information for " + movie3.title);
console.log("------------------------------");
console.log("Actors: " + movie3.actors);
console.log("Directors: " + movie3.directors);
console.log("------------------------------");
// Display the fourth movie
console.log("Movie information for " + movie4.title);
console.log("------------------------------");
console.log("Actors: " + movie4.actors);
console.log("Directors: " + movie4.directors);
console.log("------------------------------");
Listing 4.02 - Displaying information from similar objects - Task 2
- Space out the different movies.
var movie1;
var movie2;
var movie3;
var movie4;
movie1 = {
title: "Inside Out",
actors: "Amy Poehler, Bill Hader",
directors: "Pete Doctor, Ronaldo Del Carmen"
};
movie2 = {
title: "Spectre",
actors: "Daniel Craig, Christoph Waltz",
directors: "Sam Mendes"
};
movie3 = {
title: "Star Wars: Episode VII - The Force Awakens",
actors: "Harrison Ford, Mark Hamill, Carrie Fisher",
directors: "J.J.Abrams"
};
movie4 = {
title: "Tomorrowland",
actors: "George Clooney, Britt Robertson
directors: "Brad Bird"
};
console.log("Movie information for " + movie1.title);
console.log("------------------------------");
console.log("Actors: " + movie1.actors);
console.log("Directors: " + movie1.directors);
console.log("------------------------------");
console.log(""); // Add a blank line
console.log("Movie information for " + movie2.title);
console.log("------------------------------");
console.log("Actors: " + movie2.actors);
console.log("Directors: " + movie2.directors);
console.log("------------------------------");
console.log(""); // Add a blank line
console.log("Movie information for " + movie3.title);
console.log("------------------------------");
console.log("Actors: " + movie3.actors);
console.log("Directors: " + movie3.directors);
console.log("------------------------------");
console.log(""); // Add a blank line
console.log("Movie information for " + movie4.title);
console.log("------------------------------");
console.log("Actors: " + movie4.actors);
console.log("Directors: " + movie4.directors);
console.log("------------------------------");
Listing 4.02 - Displaying information from similar objects - Tasks 3 & 4
- Create objects to represent three calendar events.
- Display info from the three events on the console.
var event1 = {
title: "Adventure",
startDate: "2017-01-01",
endDate: "2017-12-31"
};
var event2 = {
title: "Space Walk",
startDate: "2016-01-15",
endDate: "2016-01-15"
};
var event3 = {
title: "Climb Mount Fuji",
startDate: "2016-03-25",
endDate: "2016-03-25"
};
console.log("*** " + event1.title + " ***");
console.log("From " + event1.startDate + " to " + event1.endDate);
console.log("");
console.log("*** " + event2.title + " ***");
console.log("From " + event2.startDate + " to " + event2.endDate);
console.log("");
console.log("*** " + event3.title + " ***");
console.log("From " + event3.startDate + " to " + event3.endDate);
console.log("");