Cash Register / Caja registradora

Hello campers,

I request your help, I don’t know why my code doesn’t happen to me, it returns two inconsistencies

  1. 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]])should return{status: “OPEN”, change: [[“TWENTY”, 60], [“TEN”, 20], [“FIVE”, 15], [“ONE”, 1], [“QUARTER”, 0.5], [“DIME”, 0.2], [“PENNY”, 0.04]]}` .

  2. checkCashRegister(19.5, 20, [[“PENNY”, 0.01], [“NICKEL”, 0], [“DIME”, 0], [“QUARTER”, 0], [“ONE”, 1], [“FIVE”, 0], [“TEN”, 0], [“TWENTY”, 0], [“ONE HUNDRED”, 0]]) should return {status: “INSUFFICIENT_FUNDS”, change: }.

This is my code…


Hola Campistas,

Solicito de su ayuda, no se por que no me pasa mi codigo me retorna dos inconsistencias

  1. 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]])should return{status: “OPEN”, change: [[“TWENTY”, 60], [“TEN”, 20], [“FIVE”, 15], [“ONE”, 1], [“QUARTER”, 0.5], [“DIME”, 0.2], [“PENNY”, 0.04]]}` .

  2. checkCashRegister(19.5, 20, [[“PENNY”, 0.01], [“NICKEL”, 0], [“DIME”, 0], [“QUARTER”, 0], [“ONE”, 1], [“FIVE”, 0], [“TEN”, 0], [“TWENTY”, 0], [“ONE HUNDRED”, 0]]) should return {status: “INSUFFICIENT_FUNDS”, change: }.

Este es mi codigo…

const REGISTER_STATUS = {closed: "CLOSED",insufficientFunds: "INSUFFICIENT_FUNDS", open:"OPEN" };

//Se almacenan las unidades monetarias dispobibles
let currencyUnit = {
  "PENNY": 0.01,
  "NICKEL": 0.05,
  "DIME": 0.10,
  "QUARTER": 0.25,
  "ONE": 1.00,
  "FIVE": 5.00,
  "TEN": 10.00,
  "TWENTY": 20.00,
  "ONE HUNDRED": 100.0
};


function checkCashRegister(price, cash, cid) {
  //Caja registradora
  let cashRegister = {status:"",change:[...cid]};
  //Cambio necesario
  let changeNeed = cash - price;
  //Cambio disponible
  let changeAvailable = getTotalCashRegisterChange(cid);
  //Mostrar en consola (saldo necesario - saldo disponoble)
  console.log("Cambio Necesario: " +changeNeed);
  console.log("Cambio Disponible: " +changeAvailable);
  //Se obtiene el estatus de la transacción del cambio
  cashRegister.status = getTotalCashRegisterStatus(changeNeed,changeAvailable);
    console.log(cashRegister.status);

// Declaración if para definir el estado insufficientFunds

if(cashRegister.status === REGISTER_STATUS.insufficientFunds) {
        cashRegister.change = [];
        return cashRegister;
    }

// Devolución
cashRegister.change = getCustomerChange(changeNeed,cid);
console.log(cashRegister.change, getTotalCashRegisterChange(cashRegister.change));

// Declaración if para definir el estado open

if(changeNeed === REGISTER_STATUS.open){
  cashRegister.change = cashRegister.change;
}

// Declaración if para definir el estado close

 if(cashRegister.status === REGISTER_STATUS.closed){
  cashRegister.change = [...cid];
}

  return cashRegister;
}

// Función para obtener el cambio del cliente

function getCustomerChange(changeNeed,changeInDrawer){
  let change = [];

// Ciclo for para definir el cambio del cliente definiendo diferentes variables para obtener el nombre de la moneda y las operaciones para retornar el cambio
  for(let i = changeInDrawer.length -1; i >= 0; i--){
    //Nombre de la moneda disponible en caja
    let coinName = changeInDrawer[i][0];
    //Total moneda en caja
    let coinTotal = changeInDrawer[i][1];
    //Tipo de moneda que se obtiene del objeto currencyUnit
    let coinValue = currencyUnit[coinName];
    //Cantidad de la moneda disponible en el cajon
    let coinAmount = (coinTotal/coinValue).toFixed(2);
    let coinToReturn = 0;
    //While que hace la operación para el cambio necesario
    while(changeNeed >= coinValue && coinAmount > 0){
         changeNeed -= coinValue;
         coinAmount--;
         coinToReturn++;
        }
        if(coinToReturn > 0){
            change.push([coinName, coinToReturn * coinValue]);
        }
  };
  if(changeNeed > 0) {
    console.log("Después de hacer el cambio, el cajón todavía está"+changeNeed+" corto.",)
}
  return change;
}

// Función para obtener el cambio total de la caja registradora

function getTotalCashRegisterChange(changeInDrawer){
    let total = 0;
  // Ciclo for para obtener el valor total del cambio total.
    for(let change of changeInDrawer){
      let changeValue = change[1];
      total += changeValue;
    }
    return total.toFixed(2);
}

// Función para obtener el valor total de la caja

function getTotalCashRegisterStatus(changeNeed,changeAvailable){
    // Declaración si para obtener en caso de no tener saldo suficiente en caja
    if(changeNeed > changeAvailable){
      return REGISTER_STATUS.insufficientFunds;
    }
  // Declaración si para obtener en caso de tener saldo suficiente en caja
    if(changeNeed < changeAvailable){
      return REGISTER_STATUS.open;
    }
    return REGISTER_STATUS.closed;
}


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]])

You have two different errors here. Sorry, I do not speak Spanish, so I hope my English answer is good enough.

For error #1: In your return object, you should have change: ["PENNY", 0.04], but you have change: ["PENNY", 0.03].

This is because your getCustomerChange function is not rounding the number properly. This is an issue with floating point numbers. Try to console.log the value of changeNeed and you will see.

Read this freeCodeCamp post for more info about how to fix it: JavaScript Cash Register Exercise - Extremely long decimals?

For error #2: Your return object should be {status: "INSUFFICIENT_FUNDS", change: []}, but you have {status: "OPEN", change: [["PENNY": 0.01]}.

You need to give the customer $0.50, and your cash register has $1.01. !BUT! You do not have the right type of coins/bills in the cash register to give the customer the exact change.

You need to work on your getTotalCashRegisterStatus function so that it will return REGISTER_STATUS.insufficientFunds. Right now it returns REGISTER_STATUS.open.

Hope that helps, and good luck.

Thank you for your valuable help camper friend.

I was able to solve the challenge… :smile:

1 Like