Get Programming with JavaScript - Listing 5.09
Listing 5.09 - Displaying a player's health
JS Bin
var showPlayerHealth;
showPlayerHealth = function (playerName, playerHealth) {
console.log(playerName + " has health " + playerHealth);
};
showPlayerHealth("Kandra", 50);
showPlayerHealth("Dax", 40);
Further Adventures
Listing 5.09 - Displaying a player's health - Task 1
- Change the showPlayerHealth function so it shows information of the form: Kandra: health 50.
var showPlayerHealth;
showPlayerHealth = function (playerName, playerHealth) {
console.log(playerName + ": health " + playerHealth);
};
showPlayerHealth("Kandra", 50);
showPlayerHealth("Dax", 40);
Listing 5.09 - Displaying a player's health - Task 2
- Call the showPlayerHealth function using your own arguments.
var showPlayerHealth;
showPlayerHealth = function (playerName, playerHealth) {
console.log(playerName + ": health " + playerHealth);
};
showPlayerHealth("Kandra", 50);
showPlayerHealth("Dax", 40);
showPlayerHealth("Jahver", 200); // Call the function
Listing 5.09 - Displaying a player's health - Tasks 3, 4 & 5
- Declare a variable called healthInfo inside the showPlayerHealth function.
- Assign healthInfo the string that will be displayed.
- Change the call to console.log so that it uses the healthInfo variable.
var showPlayerHealth;
showPlayerHealth = function (playerName, playerHealth) {
var healthInfo; // Declare a variable
healthInfo = playerName + ": health " + playerHealth; // Assign it a string
console.log(healthInfo); // Display the string
};
showPlayerHealth("Kandra", 50);
showPlayerHealth("Dax", 40);
showPlayerHealth("Jahver", 200);