2018-10-08 17:34:43 +00:00
---
id: aa2e6f85cab2ab736c9a9b24
title: Cash Register
localeTitle: Caja registradora
isRequired: true
challengeType: 5
---
## Description
2018-10-09 19:28:15 +00:00
< section id = 'description' >
Diseñe una función de cajón de la caja registradora < code > checkCashRegister()< / code > que acepte el precio de compra como primer argumento ( < code > price< / code > ), el pago como segundo argumento ( < code > cash< / code > ) y el cajón de efectivo ( < code > cid< / code > ) como tercer argumento.
< code > cid< / code > es una matriz 2D que muestra la moneda disponible.
La función < code > checkCashRegister()< / code > siempre debe devolver un objeto con una clave de < code > status< / code > y una clave de < code > change< / code > .
Devuelva < code > {status: " INSUFFICIENT_FUNDS" , change: []}< / code > si el efectivo en el cajón es inferior al cambio debido, o si no puede devolver el cambio exacto.
Devuelva < code > {status: " CLOSED" , change: [...]}< / code > con cash-in-drawer como valor del < code > change< / code > clave si es igual al cambio debido.
De lo contrario, devuelva < code > {status: " OPEN" , change: [...]}< / code > , con el cambio en monedas y billetes, ordenados de mayor a menor, como el valor de la clave de < code > change< / code > .
Recuerda usar < a href = 'http://forum.freecodecamp.org/t/how-to-get-help-when-you-are-stuck/19514' target = '_blank' > Read-Search-Ask< / a > si te atascas. Trate de emparejar el programa. Escribe tu propio código.
< table class = 'table table-striped' > < tr > < th > Unidad monetaria < / th > < th > Cantidad < / th > < / tr > < tr > < td > Centavo < / td > < td > $ 0.01 (PENNY) < / td > < / tr > < tr > < td > Níquel < / td > < td > $ 0.05 (níquel) < / td > < / tr > < tr > < td > Moneda de diez centavos < / td > < td > $ 0.1 (DIME) < / td > < / tr > < tr > < td > Trimestre < / td > < td > $ 0.25 (TRIMESTRE) < / td > < / tr > < tr > < td > Dólar < / td > < td > $ 1 (DÓLAR) < / td > < / tr > < tr > < td > Cinco dólares < / td > < td > $ 5 (CINCO) < / td > < / tr > < tr > < td > Diez dólares < / td > < td > $ 10 (DIEZ) < / td > < / tr > < tr > < td > Veinte dólares < / td > < td > $ 20 (VEINTE) < / td > < / tr > < tr > < td > Cien dolares < / td > < td > $ 100 (100) < / td > < / tr > < / table >
2018-10-08 17:34:43 +00:00
< / section >
## Instructions
2018-10-09 19:28:15 +00:00
< section id = 'instructions' >
2018-10-08 17:34:43 +00:00
< / section >
## Tests
< section id = 'tests' >
```yml
tests:
2018-10-09 19:28:15 +00:00
- text: ' < code > checkCashRegister(19.5, 20, [[" PENNY" , 1.01], [" NICKEL" , 2.05], [" DIME" , 3.1], [" QUARTER" , 4.25], [" ONE" , 90], [" FIVE" , 55], [" TEN" , 20], [" TWENTY" , 60], [" ONE HUNDRED" , 100]])</ code > debe devolver un objeto. '
2018-10-08 17:34:43 +00:00
testString: 'assert.deepEqual(Object.prototype.toString.call(checkCashRegister(19.5, 20, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]])), "[object Object]", "< code > checkCashRegister(19.5, 20, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]])< / code > should return an object.");'
2018-10-09 19:28:15 +00:00
- text: ' < code > checkCashRegister(19.5, 20, [[" PENNY" , 1.01], [" NICKEL" , 2.05], [" DIME" , 3.1], [" QUARTER" , 4.25], [" ONE" , 90], [" FIVE" , 55], [" TEN" , 20], [" TWENTY" , 60], [" ONE HUNDRED" , 100]])</ code > debe devolver < code > {status: " OPEN" , change: [[" QUARTER" , 0.5]]}</ code > . '
2018-10-08 17:34:43 +00:00
testString: 'assert.deepEqual(checkCashRegister(19.5, 20, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]]), {status: "OPEN", change: [["QUARTER", 0.5]]}, "< code > checkCashRegister(19.5, 20, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]])< / code > should return < code > {status: "OPEN", change: [["QUARTER", 0.5]]}< / code > .");'
2018-10-09 19:28:15 +00:00
- text: ' < code > 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]])</ code > debe devolver < code > {status: " OPEN" , change: [[" TWENTY" , 60], [" TEN" , 20], [" FIVE" , 15], [" ONE" , 1], [" QUARTER" , 0.5], [" DIME" , 0.2], [" PENNY" , 0.04]]}</ code > . '
2018-10-08 17:34:43 +00:00
testString: 'assert.deepEqual(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]]), {status: "OPEN", change: [["TWENTY", 60], ["TEN", 20], ["FIVE", 15], ["ONE", 1], ["QUARTER", 0.5], ["DIME", 0.2], ["PENNY", 0.04]]}, "< code > 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]])< / code > should return < code > {status: "OPEN", change: [["TWENTY", 60], ["TEN", 20], ["FIVE", 15], ["ONE", 1], ["QUARTER", 0.5], ["DIME", 0.2], ["PENNY", 0.04]]}< / code > .");'
2018-10-09 19:28:15 +00:00
- text: ' < code > checkCashRegister(19.5, 20, [[" PENNY" , 0.01], [" NICKEL" , 0], [" DIME" , 0], [" QUARTER" , 0], [" ONE" , 0], [" FIVE" , 0], [" TEN" , 0], [" TWENTY" , 0], [" ONE HUNDRED" , 0]])</ code > debe devolver < code > {status: " INSUFFICIENT_FUNDS" , change: []}</ code > . '
2018-10-08 17:34:43 +00:00
testString: 'assert.deepEqual(checkCashRegister(19.5, 20, [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]), {status: "INSUFFICIENT_FUNDS", change: []}, "< code > checkCashRegister(19.5, 20, [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]])< / code > should return < code > {status: "INSUFFICIENT_FUNDS", change: []}< / code > .");'
2018-10-09 19:28:15 +00:00
- text: ' < code > checkCashRegister(19.5, 20, [[" PENNY" , 0.01], [" NICKEL" , 0], [" DIME" , 0], [" QUARTER" , 0], [" ONE" , 1], [" FIVE" , 0], [" TEN" , 0], [" TWENTY" , 0], [" ONE HUNDRED" , 0]])</ code > debe devolver < code > {status: " INSUFFICIENT_FUNDS" , change: []}</ code > . '
2018-10-08 17:34:43 +00:00
testString: 'assert.deepEqual(checkCashRegister(19.5, 20, [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 1], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]), {status: "INSUFFICIENT_FUNDS", change: []}, "< code > checkCashRegister(19.5, 20, [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 1], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]])< / code > should return < code > {status: "INSUFFICIENT_FUNDS", change: []}< / code > .");'
2018-10-09 19:28:15 +00:00
- text: ' < code > checkCashRegister(19.5, 20, [[" PENNY" , 0.5], [" NICKEL" , 0], [" DIME" , 0], [" QUARTER" , 0], [" ONE" , 0], [" FIVE" , 0], [" TEN" , 0], [" TWENTY" , 0], [" ONE HUNDRED" , 0]])</ code > debe devolver < code > {status: " CLOSED" , change: [[" PENNY" , 0.5], [" NICKEL" , 0], [" DIME" , 0], [" QUARTER" , 0], [" ONE" , 0], [" FIVE" , 0], [" TEN" , 0], [" TWENTY" , 0], [" ONE HUNDRED" , 0]]}</ code > . '
2018-10-08 17:34:43 +00:00
testString: 'assert.deepEqual(checkCashRegister(19.5, 20, [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]), {status: "CLOSED", change: [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]}, "< code > checkCashRegister(19.5, 20, [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]])< / code > should return < code > {status: "CLOSED", change: [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]}< / code > .");'
```
< / section >
## Challenge Seed
< section id = 'challengeSeed' >
< div id = 'js-seed' >
```js
function checkCashRegister(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.1],
// ["QUARTER", 4.25],
// ["ONE", 90],
// ["FIVE", 55],
// ["TEN", 20],
// ["TWENTY", 60],
// ["ONE HUNDRED", 100]]
checkCashRegister(19.5, 20, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]]);
```
< / div >
< / section >
## Solution
< section id = 'solution' >
```js
var denom = [
{ name: 'ONE HUNDRED', val: 100},
{ name: 'TWENTY', val: 20},
{ name: 'TEN', val: 10},
{ name: 'FIVE', val: 5},
{ name: 'ONE', val: 1},
{ name: 'QUARTER', val: 0.25},
{ name: 'DIME', val: 0.1},
{ name: 'NICKEL', val: 0.05},
{ name: 'PENNY', val: 0.01}
];
function checkCashRegister(price, cash, cid) {
var output = {status: null, change: []};
var change = cash - price;
var register = cid.reduce(function(acc, curr) {
acc.total += curr[1];
acc[curr[0]] = curr[1];
return acc;
}, {total: 0});
if(register.total === change) {
output.status = 'CLOSED';
output.change = cid;
return output;
}
if(register.total < change ) {
output.status = 'INSUFFICIENT_FUNDS';
return output;
}
var change_arr = denom.reduce(function(acc, curr) {
var value = 0;
while(register[curr.name] > 0 & & change >= curr.val) {
change -= curr.val;
register[curr.name] -= curr.val;
value += curr.val;
change = Math.round(change * 100) / 100;
}
if(value > 0) {
acc.push([ curr.name, value ]);
}
return acc;
}, []);
if(change_arr.length < 1 | | change > 0) {
output.status = 'INSUFFICIENT_FUNDS';
return output;
}
output.status = 'OPEN';
output.change = change_arr;
return output;
}
```
< / section >