Hi,
So I’ve written out a rough outline for my program and divided it into different functions.
-
I needed some guidance as to how to proceed with deducting the change from the array of cash in register (cid) which is under the getChange() function.
-
Also, if anybody can figure out what I’m doing wrong in my getDenom() function where I’m trying to retrieve an object array which will display number of bills/coins that exist for each currency unit .
-
Also, I’d like to know if my usage of Objects.values(object_array) is correct?
Any other suggestions for improving my code or any tweaks to my logic would be appreciated.
Thanks.
My code so far
function checkCashRegister(price, cash, cid) {
var curr_units={
"PENNY": 0.01,
"NICKEL": 0.05,
"DIME": 0.1,
"QUARTER": 0.25,
"ONE": 1,
"FIVE": 5,
"TEN": 10,
"TWENTY": 20,
"ONE HUNDRED": 100};
var change=cash-price;
console.log("Cash-in Drawer: "+cid);
console.log("Currency Units (Values): "+Object.values(curr_units));
var new_cid=[];
for(var i=0; i<cid.length; i++){
new_cid.push(cid[i][1]);
}
console.log("Cash-in Drawer (Values):"+ new_cid);
//Register Total
var registerTotal=function(){
return new_cid.reduce((x1,x2)=>(x1+x2));
}
console.log("Register Total: "+registerTotal());
//denominations (key value pairs) (cid/curr_units) (along with keys names ()PENNY,DIME etc)
var denom=[];
var getDenom=function(new_cid,curr_units){
return Object.values(curr_units).map((x)=>(new_cid[x]/x));
}
console.log(getDenom());
//deduct change from register here
var getChange=function(change,cid,denom){
var change_due=change;
while(change_due!==0){
for(var i=denom.length; i>0; i--){
change_due=change-denom[i];
}}
return change_due;
}
//Final Status
var status = function(price, getChange){
if(change>registerTotal() || !getChange().includes(new_cid)){
return "{status: \"INSUFFICIENT_FUNDS\", change: []}";
}else if(getChange<=registerTotal()){
return "{status: \"CLOSED\", change: ["+cid+"]}";
}else{
return "{status: \"OPEN\", change: ["+getChange()+"]}";
}
}
}
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]]));
Your browser information:
User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36
.
Link to the challenge: