FCC algorithm problem--exact change

Source: Internet
Author: User

Topic:

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.

cidis 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.

When you're in trouble, remember to check for errors, read documents, search, ask questions.

Here are some resources that are helpful to you:

    • Global Object

1 functionCheckcashregister (Price, cash, CID) {2   varChange ;3   varSumcid;4   5 //Here's your change, MA ' am.6   varobj = {7Penny:cid[0][1],8Nickel:cid[1][1],9Dime:cid[2][1],TenQuarter:cid[3][1], OneOne:cid[4][1], AFive:cid[5][1], -Ten:cid[6][1], -Twenty:cid[7][1],     theOnehundred:cid[8][1] -   }; -  -   //purchase price, payment amount (cash), change in cash register (CID) +Change = cash-Price ; -   //Console.log ("typeof (change):", typeof (change)); number +   //console.log ("Change:", change); A    atSumcid = -Obj.penny + -Obj.nickel + -Obj.dime + -Obj.quarter + -Obj.one + inObj.five + -Obj.ten + toObj.twenty + + obj.onehundred; -    theSumcid = Number (sumcid.tofixed (2));//keep two decimal places *  $   //Console.log (typeof (Sumcid));//numberPanax Notoginseng   //Console.log ("Sumcid:", sumcid); -  the   if(Sumcid <Change ) { +       //Console.log ("Sumcid less than change! "); A     return"Insufficient Funds";//Insufficient Balance the   }  +   Else if(Sumcid = = =Change ) { -       //console.log ("Sumcid equals change! "); $     return"Closed"; $   }  -   Else { -       //1. Lack of change, return insufficient balance the       //2. Enough change to get the change back -       //Console.log ("Sumcid greater than change!");Wuyi overchange (change, CID, sumcid); the   }  - } Wu  - functionOverchange (change, arr, sumcid) { About   varArray = []; $   varLen =arr.length; -    -    for(vari = len-1; I >= 0; i--) { -       if(Arr[i][1] = = = 0)Continue; A  +       if(Arr[i][1] <Change ) { the Array.push (Arr[i]); -Change-= Arr[i][1]; $Change = Number (Change.tofixed (2)); the       //Console.log ("Current change:", change); the       } the  the       Else if(Arr[i][1] >Change ) { -       varx = arr[i][0];//Get change name in         //Console.log ("x:", x); the         vary = 0; the  About         Switch(x) { the            Case"One Hundred": they = Math.floor (change/100) * 100; the              Break; +            Case"Twenty": -y = Math.floor (change/20) * 20; the              Break;Bayi            Case"TEN": they = Math.floor (CHANGE/10) * 10; the              Break; -            Case"FIVE": -y = Math.floor (CHANGE/5) * 5; the                Break; the              Case"One": they = Math.floor (change/1); the                Break; -              Case"QUARTER": they = Math.floor (change/0.25) * 0.25; the                Break; the              Case"DIME":94y = Math.floor (change/0.10) * 0.10; the                Break; the              Case"Nickel": they = Math.floor (change/0.05) * 0.05;98                Break; About              Case"PENNY": -y = Math.floor (change/0.01) * 0.01;101                Break;102        }103 104 Console.log (y); theConsole.log (sumcid-arr[i][1) <Change );106 107         if(y = = = 0 && sumcid-arr[i][1] <Change ) {108         //Console.log ("Insufficient Funds");109         return"Insufficient Funds";  the         }111  the         Else if(y = = 0) {113           Continue; the         } the  the         Else {117             if(Y < 1) {118y =y;119             }            -         varArray1 = [];121Array1.push (arr[i][0]);122 Array1.push (y);123 Array.push (array1);124Change-=y; theChange = Number (Change.tofixed (2));126         //Console.log ("Current change:", change);127       } -       }129   } the 131   if(Change = = 0) { the       //console.log ("array:", array);133       returnArray;134   }135   Else {136       //Console.log ("Insufficient Funds");137       return"Insufficient Funds";138   }139    $ }141 142Checkcashregister (3.26, 100.00, [["PENNY", 1.01], ["Nickel", 2.05], ["DIME", 3.10], ["QUARTER", 4.25], ["One", 90.00], ["F IVE ", 55.00], [" TEN ", 20.00], [" Twenty ", 60.00], [" One Hundred ", 100.00]]);

checkCashRegister(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]])An array should be returned. checkCashRegister(19.50, 20.00, [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]])A string should be returned. checkCashRegister(19.50, 20.00, [["PENNY", 0.50], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]])A string should be returned. checkCashRegister(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]])should return [["QUARTER", 0.50]]. checkCashRegister(3.26, 100.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]])should return [["TWENTY", 60.00], ["TEN", 20.00], ["FIVE", 15], ["ONE", 1], ["QUARTER", 0.50], ["DIME", 0.20], ["PENNY", 0.04]]. checkCashRegister(19.50, 20.00, [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]])Should return "insufficient Funds". checkCashRegister(19.50, 20.00, [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 1.00], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]])Should return "insufficient Funds". checkCashRegister(19.50, 20.00, [["PENNY", 0.50], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]])Should return "Closed". ---------------------------------------------------------------------------------------my free Code camp address

FCC algorithm problem--exact change

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.