For this WOD you will create a sales receipt for a sale that contains at least five different products (with prices of your choosing). You must use expressions and variables to compute the various needed values dynamically. That is, do not “hard code” the computations. You should be able to change the variable values and the sales receipt will compute the correct values.
Here is an example of what your invoice should look like (you can vary the look a bit if you wish):
Item | quantity | price | extended price |
---|---|---|---|
Gillette Sensor 3 Razor | 2 | $1.23 | $2.46 |
Barbasol Shaving Cream | 1 | $2.64 | $2.64 |
Nautica Cologne | 1 | $6.17 | $6.17 |
Rubbing Alcohol | 3 | $0.98 | $2.94 |
Colgate Classic Toothbrush | 12 | $1.89 | $22.68 |
Sub-total | $36.89 | ||
Tax @ 5.75% | $2.12 | ||
Total | $39.01 |
Tasks:
Create a directory Invoice1 and add a file invoice.html. At the top of this file put a Javascript comment brifely describing what this file is. Use the /* ... */
to make a multi line comment.
// Product Data
then underneath define varaibles for just one product:
// Product 1
let item1 = 'Gillette Sensor 3 Razor';
let quantity1 = 2;
let price1 = 1.23;
Copy this and paste it 4 times. Change the variable names item2, item3, ...
and corresponding values.
Add variables extended_price1, extended_price2, ...
and corresponding expressions to compute the extended price for each product. Cooment this section // Compute extended prices
Add the products’ extended prices together to get an overall subtotal for the sale. Comment this section.
Calculate sales tax using 5.75% as your tax rate and display the tax amount. Comment this section.
Add together the subtotal and the tax amount to get a grand total. We won’t worry about shipping or other costs for now.
Add HTML to display the basic table. Create a template for one product row. to display the item, quantity, price, extended price.
Option 1: Put the row template into a document.write()
then string templates to fill in the product data in the row cells. Copy and paste this 4 times and change the variables as needed for each product line.
Option 2: Add an id="invoiceTable"
to the table. Remove the product row template. In its place use
// item row 1
let row = invoiceTable.insertRow();
row.insertCell().innerHTML = item1;
row.insertCell().innerHTML = quantity1;
row.insertCell().innerHTML = price1.toFixed(2);
row.insertCell().innerHTML = extended_price1;
Copy and paste this 4 times and adjust the variable names.
In the Sub-total row, use document.write()
inside the cell for the sub-total or set this cell id="subtotal_cell"
and then set .innerHTML
to the subtotal value. Don’t forget to add a $
sign.
In the tax row, do the same as above. Use .toFixed(2)
to format the tax to 2 decimals.
In the Total row, do the same as above and use .toFixed(2)
to format the grand total to 2 decimals. Display your grand total in bold print.
Is your code commented? |
|
Minimum five products? |
|
Prices not hard coded? |
|
Subtotal is calculated correctly? |
|
Sales tax is calculated correctly? |
|
Total is calculated correctly? |
|
Amounts only have 2 decimal places? |
|
HTML table display? |
|
Rx: <20 min Av: 30 min Sd: 40 min DNF: 40+ min
If you get stuck you can watch an example solution screencast here.