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) {
console.log("The player's name is: " + playerName); // Add extra text
};
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;
showPlayerName = function (playerName, numLetters) { // Include a second parameter
console.log("The player's name is: " + playerName);
console.log(playerName + " has " + numLetters + " letters."); // Show the number of 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);
console.log(playerName + " has " + playerName.length + " letters."); // Use the length property
};
showPlayerName("Kandra");
showPlayerName("Dax");