Get Programming with JavaScript - Listing 5.14

Listing 5.14 - Displaying a player's information using properties

var showPlayerName = function (playerName) { console.log(playerName); }; var showPlayerHealth = function (playerName, playerHealth) { console.log(playerName + " has health " + playerHealth); }; var showPlayerPlace = function (playerName, playerPlace) { console.log(playerName + " is in " + playerPlace); }; var showPlayerInfo = function (playerName, playerPlace, playerHealth) { console.log(""); showPlayerName(playerName); console.log("----------------------------"); showPlayerPlace(playerName, playerPlace); showPlayerHealth(playerName, playerHealth); console.log("----------------------------"); console.log(""); }; var player1 = { name: "Kandra", place: "The Dungeon of Doom", health: 50 }; var player2 = { name: "Dax", place: "The Old Library", health: 40 }; showPlayerInfo(player1.name, player1.place, player1.health); showPlayerInfo(player2.name, player2.place, player2.health);

Further Adventures

Listing 5.14 - Displaying a player's information using properties - Task 1

var showLine = function (length) { var line = "----------------------------------------"; // I've used 40 dashes console.log(line.substring(0, length)); }; var showPlayerName = function (playerName) { console.log(playerName); }; var showPlayerHealth = function (playerName, playerHealth) { console.log(playerName + " has health " + playerHealth); }; var showPlayerPlace = function (playerName, playerPlace) { console.log(playerName + " is in " + playerPlace); }; var showPlayerInfo = function (playerName, playerPlace, playerHealth) { console.log(""); showPlayerName(playerName); console.log("----------------------------"); showPlayerPlace(playerName, playerPlace); showPlayerHealth(playerName, playerHealth); console.log("----------------------------"); console.log(""); }; var player1 = { name: "Kandra", place: "The Dungeon of Doom", health: 50 }; var player2 = { name: "Dax", place: "The Old Library", health: 40 }; showPlayerInfo(player1.name, player1.place, player1.health); showPlayerInfo(player2.name, player2.place, player2.health);

The showLine function above can cope with lines of length between 1 and 40.

Listing 5.14 - Displaying a player's information using properties - Task 2

var showLine = function (length) { var line = "----------------------------------------"; console.log(line.substring(0, length)); }; var showPlayerName = function (playerName) { showLine(playerName.length + 4); console.log("- " + playerName + " -"); showLine(playerName.length + 4); }; var showPlayerHealth = function (playerName, playerHealth) { console.log(playerName + " has health " + playerHealth); }; var showPlayerPlace = function (playerName, playerPlace) { console.log(playerName + " is in " + playerPlace); }; var showPlayerInfo = function (playerName, playerPlace, playerHealth) { console.log(""); showPlayerName(playerName); console.log("----------------------------"); showPlayerPlace(playerName, playerPlace); showPlayerHealth(playerName, playerHealth); console.log("----------------------------"); console.log(""); }; var player1 = { name: "Kandra", place: "The Dungeon of Doom", health: 50 }; var player2 = { name: "Dax", place: "The Old Library", health: 40 }; showPlayerInfo(player1.name, player1.place, player1.health); showPlayerInfo(player2.name, player2.place, player2.health);