Get Programming with JavaScript - Listing 6.03
Listing 6.03 - Using the return value as an argument
JS Bin
var getHelloTo;
getHelloTo = function (name) {
return "Hello to " + name;
};
console.log(getHelloTo("Kandra"));
console.log(getHelloTo("Dax"));
Further Adventures
Listing 6.03 - Using the return value as an argument - Task 1
- Declare a variable called template in the function body of getHelloTo.
var getHelloTo;
getHelloTo = function (name) {
var template; // declare a variable
return "Hello to " + name;
};
console.log(getHelloTo("Kandra"));
console.log(getHelloTo("Dax"));
Listing 6.03 - Using the return value as an argument - Task 2
- Assign it the value "Hello to {{name}}".
var getHelloTo;
getHelloTo = function (name) {
var template;
template = "Hello to {{name}}"; // assign a value
return "Hello to " + name;
};
console.log(getHelloTo("Kandra"));
console.log(getHelloTo("Dax"));
Listing 6.03 - Using the return value as an argument - Task 3
- Replace the {{name}} placeholder with the value passed in as name.
var getHelloTo;
getHelloTo = function (name) {
var template;
template = "Hello to {{name}}";
// replace the placeholder
template = template.replace("{{name}}", name);
return "Hello to " + name;
};
console.log(getHelloTo("Kandra"));
console.log(getHelloTo("Dax"));
Listing 6.03 - Using the return value as an argument - Task 4
- Return template from the function.
var getHelloTo;
getHelloTo = function (name) {
var template;
template = "Hello to {{name}}";
template = template.replace("{{name}}", name);
return template; // return the filled template
};
console.log(getHelloTo("Kandra"));
console.log(getHelloTo("Dax"));