|
| 1 | +// Muffin Shop Checkout |
| 2 | +// Build a simple receipt for a muffin shop order. |
| 3 | + |
| 4 | +const shopName = 'Morning Muffins' |
| 5 | +const customerName = 'Avery' |
| 6 | +const isMember = true |
| 7 | +const muffinCount = 3 |
| 8 | +const coffeeCount = 2 |
| 9 | +const muffinPrice = 3.25 |
| 10 | +const coffeePrice = 4 |
| 11 | +const tipPercent = 0.15 |
| 12 | +const taxRate = 0.0825 |
| 13 | +const pickupMethod = 'counter' |
| 14 | + |
| 15 | +function buildDivider(char: string, length: number) { |
| 16 | + // 🐨 Build and return a divider string by repeating char |
| 17 | + // 💰 Start with let line = '' and add char in a for loop |
| 18 | + let line = '' |
| 19 | + return line |
| 20 | +} |
| 21 | + |
| 22 | +function formatMoney(amount: number) { |
| 23 | + // 🐨 Return a string like `$4.5` using a template literal |
| 24 | + return `$${amount}` |
| 25 | +} |
| 26 | + |
| 27 | +function calculateDiscount(subtotal: number, member: boolean) { |
| 28 | + // 🐨 If the customer is a member and subtotal is at least 15, |
| 29 | + // return 10% of subtotal. Otherwise return 0. |
| 30 | + return 0 |
| 31 | +} |
| 32 | + |
| 33 | +function calculateTax(subtotal: number, rate: number) { |
| 34 | + // 🐨 Return subtotal multiplied by rate |
| 35 | + return 0 |
| 36 | +} |
| 37 | + |
| 38 | +function calculateTip(totalBeforeTip: number, percent: number) { |
| 39 | + // 🐨 Return totalBeforeTip multiplied by percent |
| 40 | + return 0 |
| 41 | +} |
| 42 | + |
| 43 | +function calculatePickupFee(method: string) { |
| 44 | + // 🐨 Use a switch statement to return: |
| 45 | + // 'counter' -> 0 |
| 46 | + // 'curbside' -> 3 |
| 47 | + // 'delivery' -> 7 |
| 48 | + return 0 |
| 49 | +} |
| 50 | + |
| 51 | +let subtotal = 0 |
| 52 | +// 🐨 Set subtotal using item counts and prices |
| 53 | +// 💰 (muffinCount * muffinPrice) + (coffeeCount * coffeePrice) |
| 54 | + |
| 55 | +const discount = calculateDiscount(subtotal, isMember) |
| 56 | +const taxableAmount = subtotal - discount |
| 57 | +const tax = calculateTax(taxableAmount, taxRate) |
| 58 | +const tip = calculateTip(taxableAmount + tax, tipPercent) |
| 59 | +const pickupFee = calculatePickupFee(pickupMethod) |
| 60 | + |
| 61 | +let pickupLabel = '' |
| 62 | +// 🐨 If pickupFee is 0, set pickupLabel to 'FREE' |
| 63 | +// otherwise set it to formatMoney(pickupFee) |
| 64 | + |
| 65 | +let memberMessage = '' |
| 66 | +// 🐨 If isMember is true, set memberMessage to 'Member discount applied' |
| 67 | +// otherwise set it to 'Join the club for 10% off' |
| 68 | + |
| 69 | +let total = 0 |
| 70 | +// 🐨 Set total using subtotal, discount, tax, tip, and pickupFee |
| 71 | + |
| 72 | +const header = buildDivider('=', 32) |
| 73 | +const divider = buildDivider('-', 32) |
| 74 | + |
| 75 | +const receipt = |
| 76 | + `${header}\n` + |
| 77 | + `${shopName}\n` + |
| 78 | + `Order for ${customerName}\n` + |
| 79 | + `${divider}\n` + |
| 80 | + `Muffins: ${muffinCount} x ${formatMoney(muffinPrice)}\n` + |
| 81 | + `Coffee: ${coffeeCount} x ${formatMoney(coffeePrice)}\n` + |
| 82 | + `${divider}\n` + |
| 83 | + `Subtotal: ${formatMoney(subtotal)}\n` + |
| 84 | + `Discount: -${formatMoney(discount)}\n` + |
| 85 | + `Tax: ${formatMoney(tax)}\n` + |
| 86 | + `Tip: ${formatMoney(tip)}\n` + |
| 87 | + `Pickup fee: ${pickupLabel}\n` + |
| 88 | + `${divider}\n` + |
| 89 | + `Total: ${formatMoney(total)}\n` + |
| 90 | + `${memberMessage}\n` + |
| 91 | + `${header}` |
| 92 | + |
| 93 | +console.log(receipt) |
| 94 | + |
| 95 | +export { receipt } |
0 commit comments