I believe that my code passes all the tests well.
Your code so far
function checkCashRegister(price, cash, cid) {
let vuelto = cash - price;
let cambio = ["PENNY","NICKEL","DIME","QUARTER","ONE","FIVE","TEN","TWENTY","ONE HUNDRED"];
let valor = [0.01,0.05,0.1,0.25,1,5,10,20,100];
let caja = [0.00,0,0,0,0,0,0,0,0];
let unidades = [0,0,0,0,0,0,0,0,0];
let plataCliente = [0,0,0,0,0,0,0,0,0];
let status = "";
let restoCaja = 0;
let res = {};
let change = [];
for (let i = 0; i < cid.length; i++) {
caja[cambio.indexOf(cid[i][0])] = cid[i][1];
unidades[cambio.indexOf(cid[i][0])] = Math.ceil((cid[i][1])/valor[cambio.indexOf(cid[i][0])]) ;
};
let caja2 = [].concat(...caja);
for (let i = valor.length - 1; i >= 0; i--) {
while (vuelto >= valor[i]) {
if (unidades[i] > 0) {
vuelto -= valor[i];
unidades[i] -= 1;
plataCliente[i] += valor[i];
caja[i] = caja[i] - valor[i];
}
}
};
if (0 < vuelto && vuelto < 0.01 && unidades[0] > 0) {
plataCliente[0] += valor[0];
caja[0] -= valor[0];
vuelto = 0;
};
for (let i = 0; i < caja.length; i++) {
restoCaja += caja[i];
}
if (vuelto == 0 && restoCaja > 0) {
status += "OPEN";
} else if (vuelto > 0) {
status += "INSUFFICIENT_FUNDS";
} else if ( vuelto == 0 && restoCaja < valor[0]) {
status += "CLOSED";
};
for (let i = 0; i < caja.length; i++) {
if (plataCliente[i] > 0 && status == "OPEN") {
let array = [];
for (let x = 0; x <=1; x++) {
if (x == 0) {
array.push(cambio[i]);
} else if (x == 1) {
array.push(plataCliente[i])
}
} change.push(array);
} else if ( status == "CLOSED") {
let array2 = [];
for (let x = 0; x <=1; x++) {
if (x == 0) {
array2.push(cambio[i]);
} else if (x == 1) {
array2.push(caja2[i])
}
} change.push(array2);
}
};
let change2 = [];
for (let i = change.length - 1; i >= 0; i--) {
change2.push(change[i])
}
if ( status == "OPEN") {
res["status"] = status;
res["change"] = change2;
} else if ( status == "INSUFFICIENT_FUNDS") {
res["status"] = status;
res["change"] = [];
} else if (status == "CLOSED") {
res["status"] = status;
res["change"] = change;
}
console.log(res);
console.log(typeof res)
return res;
}
checkCashRegister(19.5, 20, [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]);
checkCashRegister(19.5, 20, [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 1], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]])
checkCashRegister(3.26, 100, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]])
Your browser information:
User Agent is: Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/111.0
Challenge Information:
JavaScript Algorithms and Data Structures Projects - Cash Register