Beginner activities based on a simple Tip Calculator.
Javascript features used in this script:
In no particular order, refactor this script to add/improve features:
- For some reason, you don't like the
*arithmetic operator being passed as aconsole.log()argument.- Declare a new
tipPercentagevariable that outputs18%(or similar) and log that new variable in theconsole.log(), instead.
- Declare a new
- You realize it might be useful to know the total payment to the server.
- Declare a new
paymentTotalvariable and set its value to thebillTotalplustip.
- Declare a new
- Last time you checked, there's no coin for
$0.00001.- Use the
Number.prototype.toFixed()method to round thetipto a suitable display currency.
- Use the
- You realize you've been tipping on the tax portion of the bill!
- Refactor the script so that it calculates the tip based on the sub total before tax.
- Stretch: This app is great, but
billTotalis fixed.- If you're running this code using Node:
- Using the
process.argvarray in Node, refactor this script so that it accepts abillTotalvalue from the command line when you run the script.
- Using the
- If you're running this code using Node:
To run tip-calculator.js from the browser:
-
Download or clone this Gist into your workspace;
-
In this new directory, create a valid
index.htmlfile; -
In the
<head>ofindex.html, link totip-calculator.jswith a<script>tag:<script src="tip-calculator.js">
-
Run
index.htmlin the browser of your choice; -
Open the Web Console to view the output.
Alternatively, if you have Node installed, tip-calculator.js can be run from the command line:
- Download or clone this Gist into your workspace;
- Navigate to this directory from the command line;
- Run the file using Node:
$ node tip-calculator.js