- More JS input/output
download folder, download the
lab05 folder. We will work with the following files:
Where to put your JS code
use of the
Begin by adding code in
coffee.js so that it displays an alert with this text:
Welcome to the Coffee Consumption Calculator. We're here to help you gauge your coffee habit.
Then test the code by displaying the
coffee.html page in the browser, and make sure it displays the alert.
Each time you add more code to
coffee.js, save and then reload the page to test the changes.
Ask the user for the cost of a cup of coffee they drink, and how many coffees they have per day (on average). Given this information, you will calculate how much money they spend on coffee per week and per month.
Use two prompts to collect these two pieces of data from the user. Save them in two variables. Remember to use meaningful names for your variables! It is the best way to self-document your code.
Calculate the amount the user spends on coffee per week, and per month (assume 4 weeks in a month).
Output the results
Output both results to the Console. Then write the results to the page itself.
Format the results
If your user enters an excessive amount of decimal points, for example
for cost, your results will also include excessive decimal points.
This method takes a single parameter to indicate how many decimal points you want. `
Pick an appropriate message
Based on how much the user spends on coffee per week, include a message to your user as part of your results. The message should correspond to the volume of their coffee consumption:
- Less than $10 a week:
That seems like a reasonable amount; enjoy!
- $10 or more, but less than $30:
Ok, that's a little pricey, but still not too bad.
- $30 or more, but less than $50:
Do you have any money leftover for food?
- $50 or more:
You'd better quit the habit before you go broke.
Another conditional (optional)
If you have enough time, add another conditional to your code. Using the cost that the user gave for a single cup of coffee, display another message:
- Less than $2/cup:
Wow, where do you buy <em>your</em> coffee?
- Between $2 and $4/cup:
Seems like you drink plain coffee.
- Between $4 and $8/cup:
Maybe you like your coffee a bit fancy?
- More than $8/cup:
Wow, you're pretty serious about your coffee.