Get Programming with JavaScript - Listing 4.13
Listing 4.13 - Updating our display function to add a blank line
var movie1;
var movie2;
var movie3;
var movie;
var showMovieInfo;
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"
};
showMovieInfo = function () {
console.log("Movie information for " + movie.title);
console.log("------------------------------");
console.log("Actors: " + movie.actors);
console.log("Directors: " + movie.directors);
console.log("------------------------------");
console.log("");
};
movie = movie1;
showMovieInfo();
movie = movie2;
showMovieInfo();
movie = movie3;
showMovieInfo();
Further Adventures
Listing 4.13 - Updating our display function to add a blank line - Task 1
- Create three objects to represent blog posts.
var post1;
var post2;
var post3;
post1 = {
title : "My Crazy Space Adventure",
author : "Philae",
created : "2015-06-21",
body : "You will not believe where I just woke up!! Only on a comet..."
};
post2 = {
title : "I'm Going On An Adventure!",
author : "BB",
created : "2012-12-12",
body : "I'm off with some dwarves and a wizard."
};
post3 = {
title: "We're Home",
author: "Han",
created: "2015-12-17",
body: "It's been a long search but, finally, we're back on The Falcon."
};
Listing 4.13 - Updating our display function to add a blank line - Tasks 2 & 3
- Create a function to display the posts on the console.
- Separate the different blog posts.
var post1;
var post2;
var post3;
var post;
var showBlog;
post1 = {
title : "My Crazy Space Adventure",
author : "Philae",
created : "2015-06-21",
body : "You will not believe where I just woke up!! Only on a comet..."
};
post2 = {
title : "I'm Going On An Adventure!",
author : "BB",
created : "2012-12-12",
body : "I'm off with some dwarves and a wizard."
};
post3 = {
title: "We're Home",
author: "Han",
created: "2015-12-17",
body: "It's been a long search but, finally, we're back on The Falcon."
};
showBlog = function () {
console.log(post.title);
console.log("By " + post.author + " on " + post.created);
console.log("--------------------");
console.log(post.body);
console.log("--------------------");
console.log("");
};
To use the showBlog function, assign a blog post object to the post variable first.
post = post1;
showBlog();
post = post2;
showBlog();
post = post3;
showBlog();