Cash register code

Tell us what’s happening:
I stuck more than one week ,the code work for just one case,any help would be much appreicated.

Your code so far

var denom=[
  {name:"ONE HUNDERED",val:100.00},
  {name:"TWENTY",val:20},
  {name:"TEN",val:10},
  {name:"FIVE",val:5},
  {name:"ONE",val:1},
  {name:"QUARTER",val:0.25},
  {name:"DIME",val:0.1},
  {name:"NICKEL",val:0.05},
  {name:"PENNY",val:0.01},
];

function checkCashRegister(price, cash, cid) {
  
  var output={status:null,change:[]};
  var change=cash- price;
  var register={total:0};
  cid.forEach( (array)=>{
    var key=array[0];
    var value=array[1];
    register[key]=value;
    register.total+=value;
  });
  
   
  //change< cid
  if(register.total<change){
    output.status="insufficient-funds";
    return output;
  }
  //change=cid
  if(register.total===change){
    output.status="closed";
    output.change=cid;
    return output;
  }
  //change >cid
  //loop through the denom array 
  var result=[];
  denom.forEach( function(subArray){
var value=0;
var currentValueInRegister=register[subArray.name];
var currencyValue=subArray.value;
while(currentValueInRegister >0 &&change>=currencyValue){
  change-=currencyValue;
  register[subArray.name]-=currencyValue;
  value+=currencyValue;

      change=Math.round(change *100)/100 ;
    }
if(value >0){
  result.push([subArray.name,value]);
}
return result;
  });
  if(result.length <1 || change > 0){
    output.status="insufficient-fund";
    return output;
  }
output.status="open";
output.change=result;
return output;
}

// Example cash-in-drawer array:
// [["PENNY", 1.01],
// ["NICKEL", 2.05],
// ["DIME", 3.1],
// ["QUARTER", 4.25],
// ["ONE", 90],
// ["FIVE", 55],
// ["TEN", 20],
// ["TWENTY", 60],
// ["ONE HUNDRED", 100]]

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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/responsive-web-design/responsive-web-design-projects/build-a-tribute-page

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make easier to read.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.

markdown_Forums

Just from glancing at you code I can see that one problem is that your status fields aren’t matching what is required by the challenge. Read the instructions carefully and look at what the correct status values are.

thank you for your help.i changed the status to capital letter,so now it works in case insufficient-funds or closed but it didn’t work when the status is open and there is change.I can’t see where is the code error.

You really need to take a closer look for typos - you have a lot. Capitalization and spelling counts.

Fixing that will get you closer to a working solution.

thank you for your advice ,I revise the code for spelling and capitalization as well as the space between +=,but it still not working in open status.

Can you post your most recent code?

var denom=[
  {name:"ONE HUNDRED",val:100.00},
  {name:"TWENTY",val:20.00},
  {name:"TEN",val:10.00},
  {name:"FIVE",val:5.00},
  {name:"ONE",val:1.00},
  {name:"QUARTER",val:0.25},
  {name:"DIME",val:0.10},
  {name:"NICKEL",val:0.05},
  {name:"PENNY",val:0.01}
]

function checkCashRegister(price, cash, cid) {
  var output={status:null, change:[]};
  var change=cash-price;
  var register={total:0};
  cid.forEach( (array)=>{
    var key=array[0];
    var value=array[1];
    register[key]=value;
    register.total+=value;
  });
  
  //change< cid
  if(register.total<change){
    output.status="INSUFFICIENT_FUNDS";
    return output;
  }
  //change=cid
  if(register.total===change){
    output.status="CLOSED";
    output.change=cid;
    return output;
  }
  cid=cid.reverse();
  //change >cid
  //loop through the denom array 
  var result=[];
 denom.forEach( function(subArray){
var value=0;
var currentValueInRegister=register[subArray.name];
var currencyValue=subArray.val;//changed from value to val
while(currentValueInRegister > 0 && change >=currencyValue){
  change-=currencyValue;
  register[subArray.name]-=currencyValue;
  value+=currencyValue;

change=Math.round(change*100)/100;

}
if (value>0){
  result.push([subArray.name,value]);
}
return result;
  })
  if(result.length < 1 ||change>0){
output.status="INSUFFICIENT_FUNDS";
return output;

  }
 else{ output.status="OPEN";
  output.change=result;
  return output;  
 }
}

// Example cash-in-drawer array:
// [[“PENNY”, 1.01],
// [“NICKEL”, 2.05],
// [“DIME”, 3.1],
// [“QUARTER”, 4.25],
// [“ONE”, 90],
// [“FIVE”, 55],
// [“TEN”, 20],
// [“TWENTY”, 60],
// [“ONE HUNDRED”, 100]]

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 the variables that you are testing in the conditional of your while loop. Determine if they are all changing as you expect.

1 Like

i did,this is the output
console.log(currencyValue)=>0.25
0.25
cosole.log(currentValueInRegister)=>4,25

Try that on on a test that you are failing like checkCashRegister(3.26, 100,...

Your logic is solid but you have an error in your while loop.

You are keeping totals of how much change is still owed, how much of each denomination you have left in drawer and how much of each denomination is paid out.

Not all of those values are updating as they should. Look at the code that updates those totals and compare it to what you are testing in the conditional.

1 Like

thank you very much,finally i did it.

I knew you could! That’s why I didn’t blurt out the answer. Way to go.