Javascript Cash Register run the test function not working correctly

I had spent a few days trying to figure out why my solution wasn’t working, running into various strange results and contradictions in the passed tests depending on my syntax.

However, on the third day of my tests, I decided to run the various check conditions on repl.it and found all of my code worked.

Am I perhaps overlooking something about the interface FCC uses?

Otherwise, I’m unsure of how to complete the challenge without coming up with a completely new solution.

Here is my code:

function checkCashRegister(price, cash, cid) {
  
  // SET UP CHANGE VARIABLE
  let changeDue = cash - price;

  // SET VALUES OF CASH TYPES:
  let cashValues = [
    {name: "ONE HUNDRED", value: 100},
    {name: "TWENTY", value: 20},
    {name: "TEN", value: 10},
    {name: "FIVE", value: 5},
    {name: "ONE", value: 1},
    {name: "QUARTER", value: 0.25},
    {name: "DIME", value: 0.10},
    {name: "NICKEL", value: 0.05},
    {name: "PENNY", value: 0.01}
  ]

  // DETERMINE AMOUNTS OF CASH IN DRAWER:
  let totalPennies = (cid[0][1] / cashValues[8].value);
  
  let unroundedNickels = (cid[1][1] / cashValues[7].value);

  let totalNickels = Number((unroundedNickels).toFixed(2));
  
  let totalDimes = (cid[2][1] / cashValues[6].value);
  
  let totalQuarters = (cid[3][1] / cashValues[5].value);
  
  let totalOnes = (cid[4][1] / cashValues[4].value);

  let totalFives = (cid[5][1] / cashValues[3].value);

  let totalTens = (cid[6][1] / cashValues[2].value);

  let totalTwenties = (cid[7][1] / cashValues[1].value);

  let totalHundreds = (cid[8][1] / cashValues[0].value);

  let cashAmounts = [
    {name: "HUNDREDS", amount: totalHundreds},
    {name: 'TWENTIES', amount: totalTwenties},
    {name: 'TENS', amount: totalTens},
    {name: 'FIVES', amount: totalFives},
    {name: 'ONES', amount: totalOnes},
    {name: 'QUARTERS', amount: totalQuarters},
    {name: 'DIMES', amount: totalDimes},
    {name: 'NICKLES', amount: totalNickels},
    {name: 'PENNIES', amount: totalPennies}
  ];

  // TOTAL AMOUNT OF CASH IN DRAWER:

  // ADD VALUES IN DRAWER
  let cidTotal = (cid[0][1] + cid[1][1] + cid[2][1] + cid[3][1] + cid[4][1] + cid[5][1] +    cid[6][1] + cid[7][1] + cid[8][1]);

  // LET THAT TOTAL EQUAL THE TRUE USED TOTAL
  let trueTotal = Number((cidTotal).toFixed(2));

  // REVERSE THE CID TO WORK BETTER IN LOOPS
  let usefulCID = cid.reverse();

  // SET UP REGISTER OBJECT
  let Register = {total: trueTotal, amounts: cashAmounts, values: cashValues, inDrawer: cid}  

  // SET UP RESULT OBJECT
  // I.E. WHAT WILL BE RETURNED
  let Result = {status: "", change: []}

  let changeArr = [];

  let totVal = 0;

  let updatedChange = 0;

  let num = 0;

  let cashObj = [];

  // ============================================================================
  // ()()()()()()()()()()()()()()[ MAIN STUFF ]()()()()()()()()()()()()()()()()()
  // ============================================================================

  if (changeDue === Register.total) {
    Result.status = "CLOSED"
    Result.change = cid;
    console.log("REGISTER TOTAL: " + Register.total)
    console.log("Change Due: " + changeDue)
    console.log("CLOSED RESULT STATUS: " + Result.status)
    console.log("CLOSED RESULT CHANGE: " + Result.change)
    return Result;
  }

  if (Register.total < changeDue) {
    Result.status = "INSUFFICIENT_FUNDS"
    return Result;
  }

  for (let i = 0; i < cashValues.length; i++) {

      let amt = Register.amounts[i].amount;

    while (Register.values[i].value <= changeDue && amt > 0) {

      num++

      console.log("CHANGE DUE: " + changeDue);

      console.log("Less than Change Due: " + Register.values[i].value)

      updatedChange = (changeDue - Number((Register.values[i].value).toFixed(2)))
      changeDue = Number((updatedChange).toFixed(2));

      amt = (Register.amounts[i].amount - num)
      console.log("Amount of " + Register.values[i].name + " left: " + amt)

      totVal += Register.values[i].value
      console.log("TOTAL VAL: " + totVal)

      if (Register.values[i].value > changeDue) {
        console.log("Max of " + Register.values[i].name + "'s Needed Reached: " + num)
        cashObj.push(Register.values[i].name)
        cashObj.push(totVal)
        console.log("Cash OBJ: " + cashObj)
        break;
      }

      else if (amt === 0) {
        console.log("zero " + Register.values[i].name + " left")
        cashObj.push(Register.values[i].name)
        cashObj.push(totVal)
        console.log("Cash OBJ: " + cashObj)
        break;
      }

      else if (totVal > 0 && changeDue === 0) {
        console.log("FINAL TOTAL VAL: " + totVal)
        cashObj.push(Register.values[i].name)
        cashObj.push(totVal)
        console.log("Cash Obj: " + cashObj)
      }

      else if (Register.values[i].value < changeDue && amt === 0 && changeDue > 0) {
        Result.status = "INSUFFICIENT_FUNDS"
        return Result;
      }

    }
          console.log("===================================")

      num = 0;
      totVal = 0;

  }

    if (Result.change < changeDue) {
    Result.status = "INSUFFICIENT_FUNDS";
    return Result;
  }

  Result.status = "OPEN"
  Result.change.push(cashObj)
  return Result;

  // ===========================================================================
  // ()()()()()()()()()()()()()()[ RETURN RESULT ]()()()()()()()()()()()()()()()
  // ===========================================================================

  

}


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

What do the failing tests say?

Most of the time the actual script just doesn’t seem to complete or the pass conditions just don’t trigger.

Not completing could verly likely be FCC terminating it because it runs slowly enough that the application thinks it might be an infinite loop.

Do you have any recommendations on how I should approach completing it then? Given it is the last challenge I need completed to earn the JS certification.

If it is being caused by a time out, then you want to look out for things like repeating calculations or methods that can be done only once, running loops longer than you have to, or creating duplication. Just scanning your code, it looks like you declared a couple dozen variables. Are you making good, efficient use of the variables you have?

All of this assumes that the logic of your solution is actually sound, which I haven’t verified, since you say that you know you are calcuating the correct results.

I tried so many times my code, I know it’s working, but I still get one error, can somebody help me?

my code:

var denominations = [
{name: 'ONE HUNDRED', value:100.00},
{name: 'TWENTY', value:20.00},
{name: 'TEN', value:10.00},
{name: 'FIVE', value:5.00},
{name: 'DOLLAR', value:1.00},
{name: 'QUARTER', value:0.25},
{name: 'DIME', value:0.10},
{name: 'NICKEL', value:0.05},
{name: 'PENNY', value:0.01}
];


function checkCashRegister(price, cash, cid) {
  var output = {status:null, change:[]};
  var change = cash - price; // what we owe to the customer
  cid = cid.reverse();  //Because they ask to deliver change in order. The denominations object starts from the Bigger to the smaller but the change must be the opposite way


var cashInDrawer = {total:0}; //Joao Carlos way, por cada elemento do cid, ele faz uma iteração.
cid.map(function(next){
    var coin_name = next[0];
    var coin_amount = next[1];
    cashInDrawer.total += coin_amount;
    cashInDrawer[coin_name] = coin_amount;
});

  //No change?
  if(cashInDrawer.total === change){
output.status = 'CLOSED';
output.change = cid;
return output;
  }

  if(cashInDrawer.total < change){
    output.status = 'INSUFFICIENT_FUNDS';
    return output;
  }


if(cashInDrawer.total > change){
var results = denominations.reduce(function(acc, next, index){ //needs to reduce the money that exists in the drawer while you give the change
var currentValue = 0;
while(cid[index][1] > 0 && change >= next.value){
  change -= next.value;
  cid[index][1]-= next.value;
  currentValue += next.value;
  change = Math.round(change *100) / 100; //This is done because JS is tricky with decimal numbers
}



if(currentValue > 0){
  acc.push([next.name, currentValue]);
}

return acc;

}, []);

if(results.length < 1 || change > 0){ //This is to avoid having the enough money to give change but not the correct coins/bills, so the cashier won't be able to give the change.
  output.status = 'INSUFFICIENT_FUNDS';
  return output
}
output.status = 'OPEN';
output.change = results;
return output
}

}




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(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(checkCashRegister(19.5, 20, [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]));

I always get error in this log in freeCodeCamp, but in other interface I get the correct answer:
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]]));