Receipt Generation

Home

Part 1

Create a JavaScript function called formatUSD that accepts a numeric input parameter called price (e.g. 4.6777), and returns that price as a string formatted with a dollar sign and rounded to two decimals (e.g "$4.68").

Invoke the function, passing a price of 4.6777, and log the resulting formatted value in the console.


Part 2

Create a JavaScript function called formatPct that accepts a numeric input parameter called percentage in decimal format (e.g. 0.0875), and returns that percentage as a string formatted with a percent sign and rounded to two decimals (e.g. "8.75%").

Invoke the function, passing a percentage of 0.0875, and log the resulting formatted value in the console.


Part 3

You are buying some groceries at the corner store. The subtotal of all the items you purchased was $19.41.

Write JavaScript code that models this concept in a variable, calculates tax using a tax rate of 8.75%, and calculates the total amount owed (equal to the subtotal plus the amount of tax due).

Log the subtotal (i.e. "$19.41"), tax rate (i.e. "8.75%"), tax owed (i.e. $1.70), and total amount (i.e. "$21.11") to the console. When logging, format all prices as USD, with dollar sign and rounded to two decimal places (using the function from Part 1), and format the tax rate as a percentage (using the function from Part 2).


Part 4

Create a JavaScript function called generateReceipt that takes input parameters called subtotal and taxRate, and returns an object datatype containing the subtotal, tax rate, tax, and total as numeric values. The function's tax rate parameter should use a default value of 0.0875.

Invoke the function, using a subtotal of $19.41, and store the resulting receipt object in a variable called receipt. Log the subtotal, tax owed, and total amount to the console. When logging, format all prices as USD, with dollar sign and rounded to two decimal places (using the function from Part 1), and format the tax rate as a percentage (using the function from Part 2).