Get Programming with JavaScript - Listing 5.07
Listing 5.07 - Displaying a player's name
var showPlayerName;
showPlayerName = function (playerName) {
console.log(playerName);
};
showPlayerName("Kandra");
showPlayerName("Dax");
Further Adventures
Listing 5.07 - Displaying a player's name - Task 1
- Update the text logged so that it is of the form: The player's name is Kandra
var showPlayerName;
showPlayerName = function (playerName) {
// Add extra text
console.log("The player's name is: " + playerName);
};
showPlayerName("Kandra");
showPlayerName("Dax");
Listing 5.07 - Displaying a player's name - Task 2
- Make the function show the number of letters in the player's name.
var showPlayerName;
// Include a second parameter
showPlayerName = function (playerName, numLetters) {
console.log("The player's name is: " + playerName);
// Show the number of letters
console.log(playerName + " has " + numLetters + " letters.");
};
showPlayerName("Kandra", 6); // Include the number of letters
showPlayerName("Dax", 3);
In the solution above we manually include the number of letters as an argument when calling the function.
Fortunately, all strings have a length property.
var showPlayerName;
showPlayerName = function (playerName) {
console.log("The player's name is: " + playerName);
// Use the length property
console.log(playerName + " has " + playerName.length + " letters.");
};
showPlayerName("Kandra");
showPlayerName("Dax");