Skip to content

Instantly share code, notes, and snippets.

@acidtone
Last active October 12, 2021 00:11
Show Gist options
  • Save acidtone/5e6fc4a981ca09a8b5b9cafa46855407 to your computer and use it in GitHub Desktop.
Save acidtone/5e6fc4a981ca09a8b5b9cafa46855407 to your computer and use it in GitHub Desktop.
Spoilers: Tip Calculator

Spoilers: Tip Calculator

  • See tip-calculator.js for example answers for these Tip Calculator activities.
version: 0.1
category: js/node
tags: spoilers
gist: https://gist.github.com/acidtone/5e6fc4a981ca09a8b5b9cafa46855407
// Default values
let billSubTotal = 40
// Stretch
// Optional: process.argv is only available in Node
// let billSubTotal = parseFloat(process.argv[2]) || 0;
// Set tip and tax rates
const tipRate = .18;
const tipPercentage = tipRate * 100 + '%';
const taxRate = 0.05;
const taxPercentage = taxRate * 100 + '%';
// Calculate tip and tax amounts
const taxAmount = billSubTotal * taxRate;
const tipAmount = billSubTotal * tipRate;
const billTotal = billSubTotal + taxAmount;
const paymentTotal = billSubTotal + taxAmount + tipAmount;
// Output results
// raw
console.log(tipAmount, taxAmount, paymentTotal);
// ES6 - Output result with template literals
console.log(`Tip for $${billSubTotal} at ${tipPercentage} tip plus ${taxPercentage} tax: $${tipAmount.toFixed(2)} tip, plus $${taxAmount.toFixed(2)} tax for a total payment of $${paymentTotal.toFixed(2)}.`);
// ES5 - Output result with concatenation
console.log('Tip for ' + billSubTotal + ' at ' + tipPercentage + ' tip plus ' + taxPercentage + ' tax: ' + tipAmount.toFixed(2) + ' tip, plus ' + taxAmount.toFixed(2) + ' tax for a total payment of ' + paymentTotal.toFixed(2) + '.');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment