Get Programming with JavaScript - Listing 6.10
Listing 6.10 - Displaying player information using objects
JS Bin
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
- Add an items property to both players.
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
- Create a getPlayerItems function to return a sensible string including the items.
var getPlayerItems = function (playerName, playerItems) {
return playerName + " has: " + playerItems;
};
Listing 6.10 - Displaying player information using objects - Task 3
- Update the getPlayerInfo function to include a call to getPlayerItems.
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
- Change the two calls to getPlayerInfo so they also pass the items property as an argument.
console.log(getPlayerInfo(player1.name, player1.place, player1.health, player1.items));
console.log(getPlayerInfo(player2.name, player2.place, player2.health, player2.items));