Why while is not working

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, :frowning: but I do want to do it

I detected 2 errors

  1. 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

  2. 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 math

    Your 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:

When I first attempted this challenge I went bald from pulling all my hair out, but painstakingly I finally completed, and you will get there too and struggling to do so and keeping at it is the sign of a good programmer.

You have over 100 lines of code so my first recommendation would be to step back and think about the problem holistically and then break that down into pieces which are similar in logic.

I know having worked on a specific piece of code for so long you want it to work, but sometimes it is best to start from scratch, and this will often be much easier in the long run rather than refactoring your existing code. (I am not saying that you need to delete your code and start over, but it may be easier rather than working with 100+ lines of code you currently have.)

Floating point numbers are almost always going to cause you problems in programming so your math may very well be fine. There are several ways to get around the floating numbers issue, and personally I used toFixed which I will link, and another approach that I have not used, but sounded interesting to me was keeping track of cents in there own respective units rather than as a fraction of a dollar. I believe it was @JeremyLT that I saw mention this method so I would be glad for him to correct me if I am describing it incorrectly.

Warning! toFixed returns a string so if you do use it make sure you change the returned value back into a number.

1 Like

I have a personal bias against string manipulation because I’m used to working in languages where string manipulation is hard, but it’s a good approach here. toFixed and an integer number of cents are the two approaches I am aware of.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.