Please break the program in to functions (make it modullar) // Constant real tax
ID: 3605153 • Letter: P
Question
Please break the program in to functions (make it modullar)
// Constant real taxRate = .0945
const taxRate = parseFloat(.0945);
//constant real tipRate = .2
const tipRate = parseFloat(.2);
//declare reaL foodCost, taxAmt, tipAmt, finalTotal
var foodCost
var taxAmt
var tipAmt
var finalTotal
// Display "Please enter your foodCost"
// Input foodCost
foodCost = prompt("Please enter your foodCost");
foodCost = parseFloat(foodCost);
taxAmt = foodCost * taxRate;
tipAmt = tipRate * (taxAmt + foodCost);
finalTotal = foodCost + taxAmt + tipAmt;
//Dispaly "the results"
//Display= ("The tax Amount is $ ", taxAmt)
//Display= ("The 20% tip is $ ", tipAmt)
//Display= ("The total cost is $ ", finalTotal)
alert("The tax amount is " + taxAmt.toFixed(2))
alert("The 20% tip is " + tipAmt.toFixed(2))
alert("The total cost is " + finalTotal.toFixed(2))
Explanation / Answer
function taxAmountCalculator(p1, p2) {
return p1 * p2; // The function returns the product of p1 and p2
}
function tipAmountCalculator(p1, p2, p3) {
return (p1 * (p2+p3)); // The function returns the product of p2 and p3 addition with p1
}
function finalAmount(p1, p2, p3) {
return p1 + p2+ p3; // The function returns the Additon of p1, p2 and p2
}
function displayResult(taxAmt, tipAmt, finalAmount){
alert("The tax amount is " + taxAmt.toFixed(2))
alert("The 20% tip is " + tipAmt.toFixed(2))
alert("The total cost is " + finalTotal.toFixed(2))
}
// Constant real taxRate = .0945
const taxRate = parseFloat(.0945);
//constant real tipRate = .2
const tipRate = parseFloat(.2);
//declare reaL foodCost, taxAmt, tipAmt, finalTotal
var foodCost
var taxAmt
var tipAmt
var finalTotal
// Display "Please enter your foodCost"
// Input foodCost
foodCost = prompt("Please enter your foodCost");
foodCost = parseFloat(foodCost);
taxAmt = taxAmountCalculator(foodCost , taxRate);
tipAmt = tipAmountCalculator(tipRate ,taxAmt ,foodCost);
finalTotal = finalAmount(foodCost , taxAmt , tipAmt);
//Dispaly "the results"
//Display= ("The tax Amount is $ ", taxAmt)
//Display= ("The 20% tip is $ ", tipAmt)
//Display= ("The total cost is $ ", finalTotal)
display_result(taxAmt, tipAmt, finalTotal)