-
Notifications
You must be signed in to change notification settings - Fork 341
Bonfire Exact Change
Created by Rafase282
Github | FreeCodeCamp | CodePen | LinkedIn | Website | E-Mail
- Difficulty: 4/5
Design a cash register drawer function that accepts purchase price as the first argument, payment as the second argument, and cash-in-drawer (cid) as the third argument.
cid is a 2d array listing available currency.
Return the string "Insufficient Funds" if cash-in-drawer is less than the change due. Return the string "Closed" if cash-in-drawer is equal to the change due.
Otherwise, return change in coin and bills, sorted in highest to lowest order.
Remember to use Read-Search-Ask if you get stuck. Try to pair program. Write your own code.
function drawer(price, cash, cid) {
var change;
// Here is your change, ma'am.
return change;
}
// Example cash-in-drawer array:
// [['PENNY', 1.01],
// ['NICKEL', 2.05],
// ['DIME', 3.10],
// ['QUARTER', 4.25],
// ['ONE', 90.00],
// ['FIVE', 55.00],
// ['TEN', 20.00],
// ['TWENTY', 60.00],
// ['ONE HUNDRED', 100.00]]
drawer(19.50, 20.00, [['PENNY', 1.01], ['NICKEL', 2.05], ['DIME', 3.10], ['QUARTER', 4.25], ['ONE', 90.00], ['FIVE', 55.00], ['TEN', 20.00], ['TWENTY', 60.00], ['ONE HUNDRED', 100.00]]);
- You have to create a program that will handle when the register does not have enough cash or will have no cash after the transaction. Other than that it needs to return an array of the change in the form of an array, so that will be a 2D array.
- Is easier to handle if you will have to close the register or if you will not have enough money to complete the transaction if you know beforehand how much money is on your register. For this it would be recommended to have a function get the information assigned to a variable.
- Life is easier when you get to know the value of each currency type in the register instead of how much money is composed of that particular currency. So be sure to watch out for that.
- You will have to get as much change from one type before moving to the next from greater value to lesser, and keep going until you have covered the whole change.
Solution ahead!
function drawer(price, cash, cid) {
// Total Amount to return to client
var totalChange = +(cash - price).toFixed(2);
//Standard currency Value
var stdCurr = [0.01, 0.05, 0.10, 0.25, 1, 5, 10, 20, 100];
//Name of current currency
var currType;
// How many of the current standard currency
var stdCurrAmount;
var currCurrency;
// Change to be returned in proper format.
var changeArr = [];
var totalCash = +cid.map(function(money) {
// Gets 1D array of values
return money[1];
}).reduce(function(cash1, cash2) {
// Reduces the values to one number
return cash1 + cash2;
// Rounds to two decimal places
}).toFixed(2);
// Handle the case where we don't have enough money or will be left without money.
if (totalChange > totalCash) {
return 'Insufficient Funds';
} else if (totalChange === totalCash) {
return 'Closed';
}
// Loops array from right to left.
for (var i = +cid.length - 1; i >= 0; i--) {
// Gets the monetary value of the current array and the type.
currCurrency = +cid[i][1].toFixed(2);
currType = cid[i][0];
//If the currency is less than the change left to hand then get the right amount from the current type of change.
if (+stdCurr[i].toFixed(2) <= +totalChange.toFixed(2)) {
stdCurrAmount = Math.floor(currCurrency / stdCurr[i]);
if ((stdCurr[i] * stdCurrAmount) >= totalChange) {
stdCurrAmount = Math.floor(totalChange / stdCurr[i]);
}
//Get the current currency to use and udate the amount left to hand out.
currCurrency = +(stdCurr[i] * stdCurrAmount).toFixed(2);
totalChange = +(totalChange - currCurrency).toFixed(2);
// Update the values so we always have the current amount left in the register.
cid[i][1] = currCurrency;
//Push the change right change to hand out
changeArr.push([currType, currCurrency]);
}
}
return changeArr;
};
- Read comments in code.
Thanks for visiting, if you like this please feel free to star my repo, follow me or even contact me about contributing as it will be a lot of work and having help would be cool.
- HTML5 and CSS
- Responsive Design with Bootstrap
- Gear up for Success
- jQuery
- Basic JavaScript
- Object Oriented and Functional Programming
- Basic Algorithm Scripting
- Basic Front End Development Projects
- Intermediate Algorithm Scripting
- JSON APIs and Ajax
- Intermediate Front End Development Projects
- Claim Your Front End Development Certificate
- Upper Intermediate Algorithm Scripting
- Automated Testing and Debugging
- Advanced Algorithm Scripting
- AngularJS (Legacy Material)
- Git
- Node.js and Express.js
- MongoDB
- API Projects
- Dynamic Web Applications
- Claim Your Back End Development Certificate
- Greefield Nonprofit Project 1
- Greefield Nonprofit Project 2
- Legacy Nonprofit Project 1
- Legacy Nonprofit Project 2
- Claim your Full Stack Development Certification
- Whiteboard Coding Interview Training
- Critical Thinking Interview Training
- Mock Interview 1
- Mock Interview 2
- Mock Interview 3