Fourth of five required projects for freeCodeCamp's JavaScript Algorithms and Data Structures certification - Build a Cash Register
Here you'll build a cash register app that will return change to the customer based on the price of the item, the amount of cash provided by the customer, and the amount of cash in the cash drawer.
You'll also need to show different messages to the user in different scenarios, such as when the customer provides too little cash or when the cash drawer doesn't have enough to issue the correct change.
Objective: Build an app that is functionally similar to cash-register.freecodecamp.rocks
In the script.js file, you have been provided with the price and cid variables. The price variable is the price of the item, and the cid variable is the cash-in-drawer, which is a 2D array listing the available currency in the cash drawer.
The other variable you will need to add is the cash variable, which is the amount of cash provided by the customer for the item, which is provided via an input element on the page.
If you'd like to test your application with different values for price and cid, make sure to declare them with the let keyword so they can be reassigned by our tests.
Your application should show different messages depending on the price of the item, the amount of cash provided by the customer, and the amount of cash in the drawer:
"Status: INSUFFICIENT_FUNDS": ifcash-in-draweris less than the change due, or if you cannot return the exact change."Status: CLOSED": ifcash-in-draweris equal to the change due."Status: OPEN": ifcash-in-draweris greater than the change due and you can return change, with the change due in coins and bills sorted in highest to lowest order.
| Currency Unit | Amount |
|---|---|
| Penny | $0.01 (PENNY) |
| Nickel | $0.05 (NICKEL) |
| Dime | $0.1 (DIME) |
| Quarter | $0.25 (QUARTER) |
| Dollar | $1 (ONE) |
| Five Dollars | $5 (FIVE) |
| Ten Dollars | $10 (TEN) |
| Twenty Dollars | $20 (TWENTY) |
| One Hundred Dollars | $100 (ONE HUNDRED) |
- You should have an
inputelement with anidof "cash" √ - You should have a
div,spanorpelement with anidof"change-due"√ - You should have a
buttonelement with anidof"purchase-btn"√ - When the value in the
#cashelement is less thanprice, an alert should appear with the text"Customer does not have enough money to purchase the item"√ - When the value in the
#cashelement is equal toprice, the value in the#change-dueelement should be"No change due - customer paid with exact cash"√
More user stories...
- When
priceis19.5, the value in the#cashelement is20,cidis[["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]], and the#purchase-btnelement is clicked, the value in the#change-dueelement should be"Status: OPEN QUARTER: $0.5" - When
priceis3.26, the value in the#cashelement is100,cidis[["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]], and the#purchase-btnelement is clicked, the value in the#change-dueelement should be"Status: OPEN TWENTY: $60 TEN: $20 FIVE: $15 ONE: $1 QUARTER: $0.5 DIME: $0.2 PENNY: $0.04" - When
priceis19.5, the value in the#cashelement is20,cidis[["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]], and the#purchase-btnelement is clicked, the value in the#change-dueelement should be"Status: INSUFFICIENT_FUNDS"√ - When
priceis19.5, the value in the#cashelement is20,cidis[["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 1], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]], and the#purchase-btnelement is clicked, the value in the#change-dueelement should be"Status: INSUFFICIENT_FUNDS"√ - When
priceis19.5, the value in the#cashelement is20,cidis[["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]], and the#purchase-btnelement is clicked, the value in the#change-dueelement should be"Status: CLOSED PENNY: $0.5"