Get Programming with JavaScript - Listing 5.13
Listing 5.13 - Displaying a player's information
JS Bin
var showPlayerInfo;
var showPlayerName;
var showPlayerHealth;
var showPlayerPlace;
showPlayerName = function (playerName) {
console.log(playerName);
};
showPlayerHealth = function (playerName, playerHealth) {
console.log(playerName + " has health " + playerHealth);
};
showPlayerPlace = function (playerName, playerPlace) {
console.log(playerName + " is in " + playerPlace);
};
showPlayerInfo = function (playerName, playerPlace, playerHealth) {
console.log("");
showPlayerName(playerName);
console.log("----------------------------");
showPlayerPlace(playerName, playerPlace);
showPlayerHealth(playerName, playerHealth);
console.log("----------------------------");
console.log("");
};
showPlayerInfo("Kandra", "The Dungeon of Doom", 50);
showPlayerInfo("Dax", "The Old Library", 40);
Further Adventures
Listing 5.13 - Displaying a player's information - Task 1
- Create a function called showLine that logs a line of dashes to the console.
var showPlayerInfo;
var showPlayerName;
var showPlayerHealth;
var showPlayerPlace;
var showLine; // Declare a variable
showPlayerName = function (playerName) {
console.log(playerName);
};
showPlayerHealth = function (playerName, playerHealth) {
console.log(playerName + " has health " + playerHealth);
};
showPlayerPlace = function (playerName, playerPlace) {
console.log(playerName + " is in " + playerPlace);
};
// Define the function and assign it
showLine = function () {
console.log("----------------------------");
};
showPlayerInfo = function (playerName, playerPlace, playerHealth) {
console.log("");
showPlayerName(playerName);
console.log("----------------------------");
showPlayerPlace(playerName, playerPlace);
showPlayerHealth(playerName, playerHealth);
console.log("----------------------------");
console.log("");
};
showPlayerInfo("Kandra", "The Dungeon of Doom", 50);
showPlayerInfo("Dax", "The Old Library", 40);
Listing 5.13 - Displaying a player's information - Task 2
- Update the showPlayerInfo function to use the showLine function rather than logging dashes to the console directly.
var showPlayerInfo;
var showPlayerName;
var showPlayerHealth;
var showPlayerPlace;
var showLine;
showPlayerName = function (playerName) {
console.log(playerName);
};
showPlayerHealth = function (playerName, playerHealth) {
console.log(playerName + " has health " + playerHealth);
};
showPlayerPlace = function (playerName, playerPlace) {
console.log(playerName + " is in " + playerPlace);
};
showLine = function () {
console.log("----------------------------");
};
showPlayerInfo = function (playerName, playerPlace, playerHealth) {
console.log("");
showPlayerName(playerName);
showLine();
showPlayerPlace(playerName, playerPlace);
showPlayerHealth(playerName, playerHealth);
showLine();
console.log("");
};
showPlayerInfo("Kandra", "The Dungeon of Doom", 50);
showPlayerInfo("Dax", "The Old Library", 40);
Listing 5.13 - Displaying a player's information - Task 3
- Create and use a showBlankLine function for logging a blank line to the console.
var showPlayerInfo;
var showPlayerName;
var showPlayerHealth;
var showPlayerPlace;
var showLine;
var showBlankLine; // Declare a variable
showPlayerName = function (playerName) {
console.log(playerName);
};
showPlayerHealth = function (playerName, playerHealth) {
console.log(playerName + " has health " + playerHealth);
};
showPlayerPlace = function (playerName, playerPlace) {
console.log(playerName + " is in " + playerPlace);
};
showLine = function () {
console.log("----------------------------");
};
// Define a function and assign it
showBlankLine = function () {
console.log("");
};
showPlayerInfo = function (playerName, playerPlace, playerHealth) {
showBlankLine();
showPlayerName(playerName);
showLine();
showPlayerPlace(playerName, playerPlace);
showPlayerHealth(playerName, playerHealth);
showLine();
showBlankLine();
};
showPlayerInfo("Kandra", "The Dungeon of Doom", 50);
showPlayerInfo("Dax", "The Old Library", 40);