Tell us what’s happening:
Hello dear community
I need your help please, I am starting to doubt if I am good enough for this, but I do want to do it
I detected 2 errors
-
WHILE NOT WORKING:
when passing:
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]])
It seems the while is not looping under the same denomination but goes to the next and spoils the whole operations, I don’t understand why the while is failing -
MATH NOT ACCURATE:
when passing:
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”, 100]])
I still miss the exercise by 1 penny, meaning I don’t manage yet to make good mathYour code so far
function checkCashRegister(price, cash, cid) {
var cashInDrawer = [];
//CONVERTING ALL ARRAYS INTO JSON//
function MoneyType(name,amountAvailable,coinsAvailable,denomination){
this.name = name;
this.amountAvailable = amountAvailable;
this.coinsAvailable = coinsAvailable;
this.denomination = denomination;
}
let cashType;
for(var i=0;i<cid.length;i++){
for(var j=0;j<cid[i].length;j++){
cashType = new MoneyType(cid[i][0],cid[i][1],)
}
cashInDrawer.push(cashType);
}
//CALCULATING NUMBER OF COINS
cashInDrawer.map(item => {
if(item.name == "PENNY"){
item.denomination = 0.01
item.coinsAvailable = item.amountAvailable/item.denomination
}else if(item.name == "NICKEL"){
item.denomination = 0.05
var value = item.amountAvailable/item.denomination
item.coinsAvailable = Math.ceil(value)
}else if(item.name == "DIME"){
item.denomination = 0.1
item.coinsAvailable = item.amountAvailable/item.denomination
}else if(item.name == "QUARTER"){
item.denomination = 0.25
item.coinsAvailable = Math.round(item.amountAvailable/item.denomination)
}else if(item.name == "ONE"){
item.denomination = 1
item.coinsAvailable = Math.round(item.amountAvailable/item.denomination)
}else if(item.name == "FIVE"){
item.denomination = 5
item.coinsAvailable = Math.round(item.amountAvailable/item.denomination)
}else if(item.name == "TEN"){
item.denomination = 10
item.coinsAvailable = Math.round(item.amountAvailable/item.denomination)
}else if(item.name == "TWENTY"){
item.denomination = 20
item.coinsAvailable = Math.round(item.amountAvailable/item.denomination)
}else if(item.name == "ONE HUNDRED"){
item.denomination = 100
item.coinsAvailable = Math.round(item.amountAvailable/item.denomination)
}
})
//console.log(cashInDrawer)
//CALCULATING TOTAL MONEY IN DRAWER
var totalInDrawer = 0;
cashInDrawer.map(item=>{
totalInDrawer = totalInDrawer + item.amountAvailable
});
// CALCULATING CHANGE TO GIVE BACK
var changeToGive = cash - price;
changeToGive.toFixed(2)
// SORTING BILLS AND COINS BY DENOMINATION
cashInDrawer.sort((a, b) => b.denomination-a.denomination);
//CREATING NEW BALANCE IN DRAWER AFTER EXTRACTING THE CHANGE
var newBalance = cashInDrawer;
var newChangeToGive = changeToGive;
let paramAmountAvailable;
// VARIABLES OF THE RESPONSE
var newArr = [];
var newTotal = 0;
/*START OF CHECKER*/
for(var l = 0;l<newBalance.length;l++){
paramAmountAvailable = newBalance[l].amountAvailable;
let parmDenomination = newBalance[l].denomination;
if(newChangeToGive/parmDenomination<=1){
console.log('Bill bigger ' + newBalance[l].name)
}else if(newChangeToGive/parmDenomination>=1){
console.log('Bill smaller ' + newBalance[l].name)
console.log('amountavailable before while ' + paramAmountAvailable)
console.log('changetoGive before while ' + newChangeToGive)
while (paramAmountAvailable > 0) {
if (paramAmountAvailable === 0 || newChangeToGive/parmDenomination<=1) {
break;
}
newChangeToGive = newChangeToGive.toFixed(2) - parmDenomination.toFixed(2);
paramAmountAvailable = paramAmountAvailable - parmDenomination.toFixed(2);
newTotal = newTotal + parmDenomination;
}
console.log('amount available after while' + paramAmountAvailable)
console.log('change to Give after while ' + newChangeToGive)
newArr.push(newBalance[l].name);
newArr.push(newTotal);
newBalance[l].amountAvailable = paramAmountAvailable;
}
newTotal = 0;
}
/*END OF CHECKER*/
console.log('nwe arr ' + newArr)
console.log("model " + [["TWENTY", 60], ["TEN", 20], ["FIVE", 15], ["ONE", 1], ["QUARTER", 0.5], ["DIME", 0.2], ["PENNY", 0.04]]);
}
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", 100]])
);
**Your browser information:**
User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36
Challenge: Cash Register
Link to the challenge: