Tell us what’s happening:
I’ve run through all the different tests and get output IDENTICAL to the expected and yet I still fail the second and third tests. I’ve been working on this one for three days and cannot see any reason why I can’t pass these two tests. I am aware that there are vastly different avenues to take in regard to this challenge, but I’m right there with my method! Any help pinpointing what it is about my method that is hanging up would be greatly appreciated.
Your code so far
//cash register
//Denominations and values
var denom = [
{name:"PENNY", value:0.01},
{name:"NICKEL", value:0.05},
{name:"DIME", value:0.10},
{name:"QUARTER", value:0.25},
{name:"ONE", value:1.00},
{name:"FIVE", value:5.00},
{name:"TEN", value:10.00},
{name:"TWENTY", value:20.00},
{name:"ONEHUNDRED", value:100.00}
];
var changeOutputArr=[];
function checkCashRegister(price, cash, cid) {
var output= {status:null, change:[]};
var change = cash - price;
var totalCid=0;
//compute value of cash in drawer and store
for(let i=0;i<cid.length;i++){
totalCid +=cid[i][1];
}
totalCid= Math.round(100*totalCid)/100;
//rounds to nearest cent
//Insufficient Funds (change>CID)
if (change>totalCid){
output.status="INSUFFICIENT_FUNDS";
return output;
}
//Closed (change == CID)
if (change===totalCid){
output.status="CLOSED";
output.change=cid;
return output;
}
//Open (change<CID)
for(let x=denom.length-1;x>=0;x--){ //loops through the denominations
//setting up a ton of variables
var billName = denom[x].name;
var billVal = denom[x].value;
var changeBillCount=0;
var changeBillVal=0;
var cidBillVal = cid[x][1]
var cidBillCount =cidBillVal/billVal;
/*if you are able to extract the denomination amount from the drawer to go towards change, then set up a number of times to loop through got that denomination.*/
if(change>billVal){
var loopCounter =Math.floor(change/billVal);
//to keep from taking more bills than available
if (loopCounter > cidBillVal/billVal){
loopCounter=Math.floor(cidBillVal/billVal);
}
/*loop though subtracting specified denomination as many times
as you can (or until you run out of that denomination bills)*/
while(loopCounter>0){
if (cidBillCount<0){break;}//you run out of bills
change-=billVal;
change=Math.round(100 * change)/100;
cidBillVal-=billVal;
changeBillCount+=1;
cidBillCount-=1;
changeBillVal=changeBillCount * billVal;
loopCounter-=1;
}//end while
}//end if
//if you used any bills of the specified denomination, push them into the change array
if(changeBillVal!==0){
changeOutputArr.push([billName,changeBillVal]);
}
}//end the big old loop
//CID does not have the proper denominations to make change
if (change>0){
output.status="INSUFFICIENT_FUNDS";
return output;}
//put it all together
output.status="OPEN";
output.change=changeOutputArr;
return output;
}//end the function
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", 400]])));
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36
.
Challenge: Cash Register
Link to the challenge: