Hi everybody, im facing with one javascript challenge.
here is the request:
Design a cash register drawer function checkCashRegister() that accepts purchase price as the first argument (price), payment as the second argument (cash), and cash-in-drawer (cid) as the third argument.
cid is a 2D array listing available currency.
The checkCashRegister() function should always return an object with a status key and a change key.
Return {status: "INSUFFICIENT_FUNDS", change: []} if cash-in-drawer is less than the change due, or if you cannot return the exact change.
Return {status: "CLOSED", change: [...]} with cash-in-drawer as the value for the key change if it is equal to the change due.
Otherwise, return {status: "OPEN", change: [...]}, with the change due in coins and bills, sorted in highest to lowest order, as the value of the change key.
__________________________________________________________________________________________________________________
Here is my actual code:
here is the request:
Design a cash register drawer function checkCashRegister() that accepts purchase price as the first argument (price), payment as the second argument (cash), and cash-in-drawer (cid) as the third argument.
cid is a 2D array listing available currency.
The checkCashRegister() function should always return an object with a status key and a change key.
Return {status: "INSUFFICIENT_FUNDS", change: []} if cash-in-drawer is less than the change due, or if you cannot return the exact change.
Return {status: "CLOSED", change: [...]} with cash-in-drawer as the value for the key change if it is equal to the change due.
Otherwise, return {status: "OPEN", change: [...]}, with the change due in coins and bills, sorted in highest to lowest order, as the value of the change key.
__________________________________________________________________________________________________________________
Here is my actual code:
JavaScript:
let array = [["ONE HUNDRED", 100],["TWENTY", 20],["TEN", 10],["FIVE", 5],["ONE", 1],["QUARTER", 0.25],["DIME", 0.1],["NICKEL", 0.05],["PENNY", 0.01]];
function checkCashRegister(price, cash, cid) {
let resto = cash - price; //x=1355 - 203.54=1151.46
let ratio100 = resto / 100; // 1151.46 / 100 = 11.5146
let pezzi100 = parseInt(ratio100) * 100; //11 * 100=1100
let resto20 = resto - pezzi100; // 1151.46 -1100 = 51.46
let ratio20= resto20 / 20; //51.46 / 20= 2.573
let pezzi20 = parseInt(ratio20) * 20; // 2 * 20 = 40
let resto10 = resto - pezzi100 - pezzi20; //1151.46 - 1100 - 40 =11.46
let ratio10 = resto10 / 10; //11.46 / 10 = 1.146
let pezzi10 = parseInt(ratio10) * 10; // 1 * 10 = 10
let resto5 = resto - pezzi100 - pezzi20 - pezzi10; //1151.46 - 1100 - 40 - 10
let ratio5 = resto5 / 5;
let pezzi5 = parseInt(ratio5) * 5;
let resto1 = resto - pezzi100 - pezzi20 - pezzi10 - pezzi5;
let ratio1 = resto1 / 1;
let pezzi1 = parseInt(ratio1) * 1;
let resto025 = resto - pezzi100 - pezzi20 - pezzi10 - pezzi5 - pezzi1;
let ratio025 = resto025 / 0.25;
let pezzi025 = parseInt(ratio025) * 0.25
let resto01 = resto - pezzi100 - pezzi20 - pezzi10 - pezzi5 - pezzi1 - pezzi025;
let ratio01 = resto01 / 0.1;
let pezzi01 = parseInt(ratio01) * 0.1
let resto005 = resto - pezzi100 - pezzi20 - pezzi10 - pezzi5 - pezzi1 - pezzi025 - pezzi01;
let ratio005 = resto005 / 0.05;
let pezzi005 = parseInt(ratio005) * 0.05;
let resto001 = resto - pezzi100 - pezzi20 - pezzi10 - pezzi5 - pezzi1 - pezzi025 - pezzi01 - pezzi005;
let ratio001 = resto001 / 0.01;
let pezzi001 = parseInt(ratio001) * 0.01;
if (price = cash) {
var firstObj = { status: "CLOSED", change: cid}
return firstObj
}
else {
for(let i = 0; i < cid.length; i++) {
for (let j =0; j < cid[i].length; j++) {
var sommaCassa = reduce(cid[j])
if ( sommaCassa > resto && cash < price) {
array[i].pop()
array[0].push(pezzi100)
array[1].push(pezzi20)
array[2].push(pezzi10)
array[3].push(pezzi5)
array[4].push(pezzi1)
array[5].push(pezzi025)
array[6].push(pezzi01)
array[7].push(pezzi005)
array[8].push(pezzi001)
var secondObj = { status: "OPEN", change: array }
return secondObj;
}
}
if (sommaCassa < resto) {
var terzoObj = {status: "INSUFFICIENT_FUNDS", change: cid}
return terzoObj;
}
}}}