Get Programming with JavaScript - Listing 6.10

Listing 6.10 - Displaying player information using objects

var getPlayerName = function (playerName) { return playerName; }; var getPlayerHealth = function (playerName, playerHealth) { return playerName + " has health " + playerHealth; }; var getPlayerPlace = function (playerName, playerPlace) { return playerName + " is in " + playerPlace; }; var getBorder = function () { return "================================"; }; var getPlayerInfo = function (playerName, playerPlace, playerHealth) { var playerInfo; playerInfo = "\n" + getPlayerName(playerName); playerInfo += "\n" + getBorder(); playerInfo += "\n" + getPlayerPlace(playerName, playerPlace); playerInfo += "\n" + getPlayerHealth(playerName, playerHealth); playerInfo += "\n" + getBorder(); playerInfo += "\n"; return playerInfo; }; var player1 = { name: "Kandra", place: "The Dungeon of Doom", health: 50 }; var player2 = { name: "Dax", place: "The Old Library", health: 40 }; console.log(getPlayerInfo(player1.name, player1.place, player1.health)); console.log(getPlayerInfo(player2.name, player2.place, player2.health));

Further Adventures

Listing 6.10 - Displaying player information using objects - Task 1

var player1 = { name: "Kandra", place: "The Dungeon of Doom", health: 50, items: "a rusty key, a piece of cheese" }; var player2 = { name: "Dax", place: "The Old Library", health: 40, items: "a diamond gauntlet" };

Listing 6.10 - Displaying player information using objects - Task 2

var getPlayerItems = function (playerName, playerItems) { return playerName + " has: " + playerItems; };

Listing 6.10 - Displaying player information using objects - Task 3

var getPlayerInfo = function (playerName, playerPlace, playerHealth, playerItems) { var playerInfo; playerInfo = "\n" + getPlayerName(playerName); playerInfo += "\n" + getBorder(); playerInfo += "\n" + getPlayerPlace(playerName, playerPlace); playerInfo += "\n" + getPlayerHealth(playerName, playerHealth); playerInfo += "\n" + getPlayerItems(playerName, playerItems); playerInfo += "\n" + getBorder(); playerInfo += "\n"; return playerInfo; };

Listing 6.10 - Displaying player information using objects - Task 4

console.log(getPlayerInfo(player1.name, player1.place, player1.health, player1.items)); console.log(getPlayerInfo(player2.name, player2.place, player2.health, player2.items));