Is the 5th and 6th test case wrong?

Tell us what’s happening:

I believe the code is working well but the fifth case and the sixth case are popping errors. When one analyses the expected results they seem to be erroneous. The fifth, for example, does have enough funds but not the correct currency( it has 1 penny and 1 dollar). Something similar happens in the sixth test. Please help.

   **Your code so far**

function checkCashRegister(price, cash, cid) {

 let changeX = {status:'', change:[]};
 // currrency X 100
 let currency = [
   ["PENNY", 1],
   ["NICKEL", 5],
   ["DIME", 10],
   ["QUARTER", 25],
   ["ONE", 100],
   ["FIVE", 500],
   ["TEN", 2000],
   ["TWENTY", 6000],
   ["ONE HUNDRED", 10000]
   ]

//check if there is money in the cash register
   let money = 0;

   for(let i = 0 ; i < cid.length; i++)
   {
     money += cid[i][1];
   }

 //console.log(money.toFixed(2));

 let back = (cash - price).toFixed(2);

 if (back > money)
 {
   changeX.status = "INSUFFICIENT_FUNDS";
   return changeX;
 }
 
//return the propper amount - multiply X 100 to avoid rounding errors. Hate float numbers
 let current = back*100;
 let valuesUsed = 0;
 

 for(let i = cid.length - 1; i >= 0; i--)
 { 
   //check if there is money
   if (cid[i][1] > 0)
   {
     if(current % currency[i][1] < current)
     {
       //Use as much as you can of that highest currency and determine how much you can use
       let cur = Math.round(cid[i][1] / (currency[i][1] / 100));
       let min = Math.floor(current / currency[i][1]);
       changeX.change.push(currency[i]);

       //if the amout needed is lower than what is there then procede otherise use all there is
       if (min <= cur)
       {
         //console.log(cur + ' - current -> ' + current + ' min ' + min + ' ---- ' + i + ' --c-- ' + currency[i][1]);
         current -= (min * currency[i][1]);
         changeX.change[valuesUsed][1] = ((min * (currency[i][1]/100)).toFixed(2))*1;
         //multiply by 1 to avoid trailing zeros
         valuesUsed ++;

       }
       else
       {
         current -= (cur * currency[i][1]);
         changeX.change[valuesUsed][1] = ((cur * (currency[i][1]/100)).toFixed(2))*1;
         valuesUsed ++;
       }
     }
   }
 }

if (current === 0)
{
 changeX.status = "OPEN";
}
else
{
 changeX.status = "CLOSED";
}

 console.log(changeX);
 
 return changeX;
}

checkCashRegister(19.5, 20, [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 1], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]);
   **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:86.0) Gecko/20100101 Firefox/86.0.

Challenge: Cash Register

Link to the challenge:

5th Test

The fifth test gives you a situation where you have enough money, but cannot return exact change because you do not have the correct denominations. It looks like the code you’ve written doesn’t account for that, and instead returns the $1 for the change.

6th Test

This one is a quick fix. Before you loop through the change array and see what money you have, you should check if the amount of change you need to give back is the same as the total amount of money in your register. If it is, then you can return early.

Hint:

You already have the variables you need to do this - you use them when you check if you have less money than the change due.

2 Likes

Thank you very much for your answer. Although I did manage to resolve the issue i still think the instructions for this exercise are ambiguous and not explicit enough. That being said. Thank you for your time.

1 Like