Get Programming with JavaScript - Listing 5.04
Listing 5.04 - Calling the same function with different arguments
var showMessage;
showMessage = function (message) {
console.log("The message is: " + message);
};
showMessage("It's full of stars!");
showMessage("Hello to Jason Isaacs");
showMessage("Hello to Jason Isaacs and Stephen Fry");
Further Adventures
Listing 5.04 - Calling the same function with different arguments - Task 1
- Change the showMessage function to display its prefixed text on a separate line to the message.
var showMessage;
showMessage = function (message) {
console.log("The message is:"); // Display the prefix
console.log(message); // Display the message
};
showMessage("It's full of stars!");
showMessage("Hello to Jason Isaacs");
showMessage("Hello to Jason Isaacs and Stephen Fry");
You can also use the new-line escape sequence:
var showMessage;
showMessage = function (message) {
console.log("The message is:\n" + message); // Use a new-line character
};
showMessage("It's full of stars!");
showMessage("Hello to Jason Isaacs");
showMessage("Hello to Jason Isaacs and Stephen Fry");
Listing 5.04 - Calling the same function with different arguments - Task 2
- Declare a myMessage variable and assign it a string value.
var showMessage;
var myMessage; // Declare the variable
myMessage = "We choose to go."; // Assign it a value
showMessage = function (message) {
console.log("The message is:");
console.log(message);
};
showMessage("It's full of stars!");
showMessage("Hello to Jason Isaacs");
showMessage("Hello to Jason Isaacs and Stephen Fry");
Listing 5.04 - Calling the same function with different arguments - Task 3
- Call the showMessage function with myMessage as the argument.
var showMessage;
var myMessage;
myMessage = "We choose to go.";
showMessage = function (message) {
console.log("The message is:");
console.log(message);
};
showMessage("It's full of stars!");
showMessage("Hello to Jason Isaacs");
showMessage("Hello to Jason Isaacs and Stephen Fry");
showMessage(myMessage); // Use myMessage as an argument