checkCashRegister seems to not check the proper answer

const register = [
  ["PENNY", 1.01],
  ["NICKEL", 2.05],
  ["DIME", 3.1],
  ["QUARTER", 4.25],
  ["ONE", 90],
  ["FIVE", 55],
  ["TEN", 20],
  ["TWENTY", 60],
  ["ONE HUNDRED", 100]
];

class registerStatus {
  constructor (str) {
    this.str  = str;
  }
}

//  -----------------------------------------------------
//  -----------------------------------------------------
function checkCashRegister(price, cash, register) {
  const value       = [0.01, 0.05, 0.10, 0.25, 1.00, 5.00, 10.00, 20.00, 100.00].reverse();
  let dic           = register.reverse ();
  let out           = [];
  let statusClosed  = '{status: "CLOSED", change: [';
  let statusOpen    = '{status: "OPEN", change: [';
  let statusInsuff  = '{status: "INSUFFICIENT_FUNDS", change: []}';
//  -----------------------------------------------------
  for (let i = 0; i < dic.length; i++) {
    dic [i].push (Math.round(dic [i][1] / value [i]), value [i]);
  }
//  -----------------------------------------------------
  let totalCash = 0;
//  -----------------------------------------------------
  for (let  i = 0;  i < dic.length; i++)
    totalCash += dic [i][1];
//  -----------------------------------------------------
  let changeDue  = cash - price;
//  -----------------------------------------------------
  if (totalCash - changeDue < 0)
    return statusInsuff;
//  -----------------------------------------------------
  out = makeChangeList (dic, out, changeDue);
//  -----------------------------------------------------
  let size          = out.length;
  let open          = true;
  let list          = "";
  let totalChange   = 0;
//  -----------------------------------------------------
  for (let i = 0; i < size; i++)
    totalChange += out [i][3];
//  -----------------------------------------------------
  totalChange = totalChange.toFixed(2);
//  -----------------------------------------------------
  if (totalChange == changeDue)  {
    let outNumber = 0;
//  -----------------------------------------------------
    for (let i = 0; i < size; i++) {
      outNumber += (out[i][1]) ? 1 : 0;
//  -----------------------------------------------------
      if (out[i][3] == dic[out[i][2]][2])
        open = false;
    }
//  -----------------------------------------------------
    if (!open)
      out.reverse ();  
//  -----------------------------------------------------
    for (let i = 0; i < size; i++) {
//  -----------------------------------------------------
      if (open) {
        let type  = 1;
        if (out[i][1] != 0) {
          type++;
          list += "[" + inQuotes(out[i][0]) + ", " + out[i][3] + "]";
//  -----------------------------------------------------
          if ((i < size - 1) && type < outNumber) {
            list += ", ";
          } else {
            list += ""
          }
        }
      } else {
        list += "[" + inQuotes(out[i][0]) + ", " + out[i][3] + "]";
        if (i < size - 1)
          list += ", ";
        else
          list += ""
      }
    }
    list += "]}";
  }
//  -----------------------------------------------------
  if (open)
//  return  new registerStatus(statusOpen    + list);
    return  statusOpen    + list;
  else
//  return  new registerStatus(statusClosed  + list);
    return  statusClosed  + list;
}

//  -----------------------------------------------------
//  -----------------------------------------------------
function  inQuotes(str) {
  return  '"' + str + '"';
}

//  -----------------------------------------------------
//  -----------------------------------------------------
function makeChangeList(dic, out, changeDue) {
  let change  = changeDue + 0.0001;
//  -----------------------------------------------------
  for (let  i = 0;  i < dic.length; i++) {
    let number    = 0;
    let available = dic [i][2];
//  -----------------------------------------------------
    while (change >= dic [i][3] && number <= available - 1) {
      change -= dic [i][3];
      number++;
    }
//  -----------------------------------------------------
    if (number > 0) {
      let total = dic [i][3] * number;
      out.push ([dic [i][0], number, i, total]);   
    } else {
      out.push ([dic [i][0], 0, i, 0]); 
    }
  }
  return out;
}

console.log("======= 19.5, 20 ==========");
console.log(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]]));
console.log('{status: "OPEN", change: [["QUARTER", 0.5]]}');
console.log("=================");
console.log("======= 3.26, 100 ==========");
console.log(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]]));
console.log('{status: "OPEN", change: [["TWENTY", 60], ["TEN", 20], ["FIVE", 15], ["ONE", 1], ["QUARTER", 0.5], ["DIME", 0.2], ["PENNY", 0.04]]}');
console.log("=================");
console.log("========19.5, 20=========");
console.log(checkCashRegister(19.5, 20, [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]));
console.log('{status: "INSUFFICIENT_FUNDS", change: []}');
console.log("=================");
console.log("========19.5, 20=========");
console.log(checkCashRegister(19.5, 20, [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]));
console.log('{status: "CLOSED", change: [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]}');

The tests are correct. Maybe you can talk about your code and what’s going on?

You are supposed to return an object, not a string.

Even if the tests allowed for that it isn’t the correct way to work with objects.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.