Cash Register project - returned objects not passing?

Cash Register project - returned objects not passing?
0

#1

Hey, could someone take a look at my code? I keep getting stuck on returning the results. I done most of the coding in Codepen.io and in it’s console log I meet the criteria for the tests. But in the FCC console I’m not passing the tests on the “OPEN” and “INSUFFICIENT_FUNDS - not the right change” criteria. Is this a FCC console bug or am I missing something?

Here’s my code:

function checkCashRegister(price, cash, cid) {

//Variable to store change
let change = [];
let refund = {status: “”, change: []};

//Calculate amount of change
let remainder;
let cidTotal;
let amount = 0;

//Array to store cid
let conversion =

  [{unit: "ONE HUNDRED", value:100, quantity: 0},
  {unit: "TWENTY", value:20, quantity: 0},
  {unit: "TEN", value:10, quantity: 0},
  {unit: "FIVE", value:5, quantity: 0},
  {unit: "ONE", value:1, quantity: 0},
  {unit: "QUARTER", value:0.25, quantity: 0},
  {unit: "DIME", value:0.1, quantity: 0},
  {unit: "NICKEL", value:0.05, quantity: 0},
  {unit: "PENNY", value:0.01, quantity: 0}];

//Sort cid into conversion array, by reversing the cid array so the order goes from highest to lowest
function sortCid()
{
let reversedCid = cid.reverse();

for (let i = 0; i < cid.length; i++ )
  {
    conversion[i].quantity = reversedCid[i][1] / conversion[i].value;
  }

}

// Calculate total in register
function regTotal(){
cidTotal = cid.reduce((start, item) =>
{
return start += item[1];
},0.0);
console.log ("Register total: " + cidTotal);
}

//Calculate amount that needs to be returned
function difference()
{
remainder = cash - price;
console.log ("To be returned: " + remainder);
}

//Run the calculations
sortCid();
regTotal();
difference();

//check if there’s enough in the register for a change
if (remainder > cidTotal)
{
refund.status = “INSUFFICIENT_FUNDS”;
refund.change = [];
return refund;
}

if (remainder === cidTotal)
{
refund.status = ‘CLOSED’;
refund.change = cid.reverse();
return refund;
}

if (remainder < cidTotal)
{
sortChange ();
}

//Calculate and sort the change
function sortChange ()
{
for (let j = 0; j < cid.length; j++ )
{
amount = 0;
while ( conversion[j].value <= remainder && conversion[j].quantity > 0 )
{
remainder -= conversion[j].value;
remainder = Math.round(remainder * 100) / 100;
amount += conversion[j].value;
conversion[j].quantity -= 1;
}

       if (amount > 0)
       {
         change.push([conversion[j].unit, amount]); 
       }
       
    }
   
   if (remainder > 0) 
   {
      refund.status = "INSUFFICIENT_FUNDS"; 
      console.log (refund); //console log prints: Object { change: [], status: "INSUFFICIENT_FUNDS"} But FCC console doesn't register this as a pass
      return refund; 
   }   
  
  console.log (change); // Prints array previously calculated 
  refund.status = "OPEN";
  
  console.log (refund); // Prints an empty array
  return (refund);
}

}

checkCashRegister(19.5, 20, [[“PENNY”, 0.01], [“NICKEL”, 0], [“DIME”, 0], [“QUARTER”, 0], [“ONE”, 1], [“FIVE”, 0], [“TEN”, 0], [“TWENTY”, 0], [“ONE HUNDRED”, 0]]);


#2

For the closed tests, there is no return statement in the scope of the checkCashRegister() function for that case, so it is returning undefined in those cases.

For the open cases where change is due something is wrong since what is being returned is {status: “OPEN”, change: Array(0)}


#3

As a side note I think it could be helpful for debugging and readability if you try to make more “pure functions” or functions that take in parameters and return consistent results without changing variables at a higher level of their scope.