Get Programming with JavaScript - Listing 6.05
Listing 6.05 - A function with three arguments
JS Bin
var totalCost;
totalCost = function (callOutCharge, costPerHour, numberOfHours) {
return callOutCharge + costPerHour * numberOfHours;
};
console.log("$" + totalCost(30, 40, 3));
Further Adventures
Listing 6.05 - A function with three arguments - Task 1
- What is the cost for 12 hours work?
var totalCost;
totalCost = function (callOutCharge, costPerHour, numberOfHours) {
return callOutCharge + costPerHour * numberOfHours;
};
console.log("$" + totalCost(30, 40, 12)); // set numberOfHours to 12
// logs $510
Listing 6.05 - A function with three arguments - Task 2
- Add a fourth parameter to the totalCost function definition to account for discounts.
var totalCost;
// include a fourth parameter
totalCost = function (callOutCharge, costPerHour, numberOfHours, discount) {
return callOutCharge + costPerHour * numberOfHours;
};
console.log("$" + totalCost(30, 40, 3));
Listing 6.05 - A function with three arguments - Task 3
- Update the function to subtract the discount from the total before returning it.
var totalCost;
totalCost = function (callOutCharge, costPerHour, numberOfHours, discount) {
// apply the discount
return callOutCharge + costPerHour * numberOfHours - discount;
};
console.log("$" + totalCost(30, 40, 3));
Listing 6.05 - A function with three arguments - Task 4
- If a customer has a $20 off coupon, update the call to totalCost to display the total cost.
var totalCost;
totalCost = function (callOutCharge, costPerHour, numberOfHours, discount) {
return callOutCharge + costPerHour * numberOfHours - discount;
};
// apply a $20 discount
console.log("$" + totalCost(30, 40, 3, 20));