Exact Change
Design a Cashier program checkCashRegister()
that takes the purchase price ( price
) as the first parameter, the payment amount ( cash
) as the second parameter, and the cash register in the Change ( cid
) as the third parameter.
cid
is a two-dimensional array with the current available change.
When there's not enough money in the register. 0 O'Clock returns a string. "Insufficient Funds"
if it happens, the string is returned "Closed"
.
Otherwise, the list of changes that should be retrieved is returned, and there are two-dimensional arrays from large to small.
Global Object
Code:
1 functionCheckcashregister (Price, cash, CID) {2 varobj = {};3 varnum = 0;4 varOdd = cash-Price ;5 vararr = [];6 varNEWARR = [];7 varA = 0;8 9 for(vari = 0; i < cid.length; i++) {Ten //change the first and second entries of this two-dimensional array to the properties and property values of the new object. One vars = cid[i][0]; AObj[s] = cid[i][1]; - //Add up The sum of the register change -num + = cid[i][1]; the } - if(Odd = = =num) { - //returns the string "Closed" if the money in the register is odd equal to the money that needs to be found - return"Closed"; + } - for(varj = 0; J < Object.keys (obj). length; J + +) { + //Traversing Object Properties A varName = Object.keys (obj) [8-j]; at //create a new array of dollar amounts - varFigure = [100, 20, 10, 5, 1, 0.25, 0.1, 0.05, 0.01]; - - fi1 (); - - if(A > 0) { inArr.push ([Name, Figure[j] *a]); -A = 0; to } + } - functionFi1 () { the if(Odd >= Figure[j] && Obj[name] >=Figure[j]) { * //to judge a odd, if it is larger than a certain denomination and has a banknote of that denomination on the radio $Odd = odd-Figure[j];Panax Notoginseng //js accuracy problem, 2=1.999999, so to use Math.Round () to round -Odd = Math.Round (odd * 10000)/10000; theObj[name] = Obj[name]-Figure[j]; +Obj[name] =Math.Round (Obj[name]); A //this A is used to determine if there are multiple banknotes of uniform face value, which is added in the IF statement above to the empty array arr . theA + = 1; + fi1 (); - } $ } $ if(Odd = = 0) { - //If you can change and have the rest, return a list of changes that should be recovered - returnarr; the}Else if(Odd > 0) { - //returns the string "Insufficient Funds" if the money in the radio is not enough to find zeroWuyi return"Insufficient Funds"; the } - } Wu -Checkcashregister (19.50, 20.00, [ About["PENNY", 0.50], $["Nickel", 0], -["DIME", 0], -["QUARTER", 0], -["One", 0], A["FIVE", 0], +["TEN", 0], the["Twenty", 0], -["One Hundred", 0] $]);
FCC Advanced Algorithm problem Register find change