/* Present the user with a welcome message. */ function welcome() {
`;
console.log(msg);
}
/* Calculate the tip on a bill, given the pct of the tip. Return the amount of the tip */ function calcTip(bill, pct) { // convert pct to a decimal and calculate let tip = bill * pct;
// convert the tip to float with 2 decimal places
tip = Number.parseFloat(tip);
return tip;
}
/* Ask the user to enter the amount of the bill and return this amount as a */ function askBillAmt() { let amt = window.prompt("How much was your total bill?"); amt = Number.parseFloat(amt);
return amt;
}
/* Allow the user to choose a tip amount from a menu. */ function askTipPct() { let pct = window.prompt("What percent tip do you want to leave?"); // convert the pct from a whole number to a fraction pct /= 100; pct = Number.parseFloat(pct).toFixed(2); return pct; }
function money(amt) { let dollars = "$" + Number.parseFloat(amt).toFixed(2); return dollars; }
/*
Prints a message to the user showing
the result of the calculations.
*/
function showResults(bill, tip, pct) {
let total = tip + bill;
console.log("Bill amount: " + money(bill) ); // multiply % by 100 to convert back console.log("Tip percentage: " + (pct * 100) + "%" ); console.log("Tip amount due: " + money(tip) ); console.log("Total with tip: " + money(total) );
GOOD BYE
`); }
/* Read in the basic information, calcualte the tip and the share, then dispaly the results to the user. */ function main() {
welcome();
let myBill = askBillAmt();
let pct = askTipPct();
let tip = calcTip(myBill, pct);
showResults(myBill, tip, pct);
}
main();
</div>