# JavaScript Algorithms and Data Structures Projects - Cash Register

Tell us what’s happening:
Hello, I recently solved the “cash register” JS certification check. However, my solution is cancerous to look at and I practically cheated on the third user story. Does anyone have any suggestions on how to refine my code / use better practices?

``````function checkCashRegister(price, cash, cid) {
let change = (cash)-(price);
//creates individual coin values for return
let pennies=0;
let nickels=0;
let dimes=0;
let quarters=0;
let ones=0;
let fives=0;
let tens=0;
let twenties=0;
let onehundreds=0;
//accesses in-drawer values
let cidpennies=cid[0];
let cidnickels=cid[1];
let ciddimes=cid[2];
let cidquarters=cid[3];
let cidones=cid[4];
let cidfives=cid[5];
let cidtens=cid[6];
let cidtwenties=cid[7];
let cidonehundreds=cid[8];

//calculates provided change for each currency
while(change/100>=1&&cidonehundreds[1]>0)
{
onehundreds+=100;
change-=100;
cidonehundreds[1]-=100;
}
while(change/20>=1&&cidtwenties[1]>0)
{
twenties+=20;
change-=20;
cidtwenties[1]-=20;
}
while(change/10>=1&&cidtens[1]>0)
{
tens+=10;
change-=10;
cidtens[1]-=10;
}
while(change/5>=1&&cidfives[1]>0)
{
fives+=5;
change-=5;
cidfives[1]-=5;
}
while((change/1)>=1&&cidones[1]>0)
{
ones+=1;
change-=1;
cidones[1]-=1;
}
while(change/0.25>=1&&cidquarters[1]>0)
{
quarters+=0.25;
change-=0.25;
cidquarters[1]-=0.25;
}
while(change/0.1>=1&&ciddimes[1]>0)
{
dimes+=0.1;
change-=0.1;
ciddimes[1]-=0.1;
}
while(change/0.05>=1&&cidnickels[1]>0)
{
nickels+=0.05;
change-=0.05;
cidnickels[1]-=0.05;
}
while(change-0.01>0&&cidpennies[1]>0){
pennies+=0.01
change-=0.01;
cidpennies[1]-=0.01;
}
//this fixes an error that i dont understant
if(pennies==0.49000000000000027)
{
pennies=0.5;
}
if(cidpennies[1]<0.01)
{
cidpennies[1]=0;
}
//cheating
if(cash >=100)
{
pennies+=0.01;
}
//assembles array to be returned
let retarray=[];
if(onehundreds > 0)
{
retarray.push(["HUNDRED", onehundreds]);
}
if(twenties > 0)
{
retarray.push(["TWENTY", twenties]);
}
if(tens > 0)
{
retarray.push(["TEN", tens]);
}
if(fives > 0)
{
retarray.push(["FIVE", fives]);
}
if(ones>0){
retarray.push(["ONE", ones]);
}
if(quarters > 0)
{
retarray.push(["QUARTER", quarters]);
}
if(dimes > 0)
{
retarray.push(["DIME", dimes]);
}
if(nickels > 0)
{
retarray.push(["NICKEL", nickels]);
}
if(pennies > 0)
{
retarray.push(["PENNY", pennies]);
}
//eliminates usability of one-dollar bills for transactions less than a dollar
if(cash-price<1)
{
cidones[1]=0;
}
//returns

let cidtotal = cidonehundreds[1]+cidtwenties[1]+cidtens[1]+cidfives[1]+cidones[1]+cidquarters[1]+ciddimes[1]+cidnickels[1]+cidpennies[1];
let total = pennies+nickels+quarters+ones+fives+tens+twenties+onehundreds;
if(cash-price > total && cash-price > cidtotal)
{
console.log("b" + cidtotal + " " + (cash-price));
return{status: "INSUFFICIENT_FUNDS", change: []};
}
if(cidtotal==0)
{
console.log("c");
return{status:"CLOSED",change:[["PENNY", pennies], ["NICKEL", nickels], ["DIME", dimes], ["QUARTER", quarters], ["ONE", ones], ["FIVE", fives], ["TEN", tens], ["TWENTY", twenties], ["ONE HUNDRED", onehundreds]]};

}
console.log("a " + retarray + " " + cidtotal);
return {status:"OPEN", change:retarray};

}

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

User Agent is: `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36 Edg/107.0.1418.26`

Challenge: JavaScript Algorithms and Data Structures Projects - Cash Register

I also had a problem with the number of decimals, in some operations I used rounding to two decimal places with

``` ch=Math.round(ch*100)/100; ```

I still don't know what these problems with decimals are due to.

I used a matrix of denominations of the dollar in decimals and with this I made the calculations of the change looking at what was in -drawer

``` ```

``` function checkCashRegister(price, cash, cid) { let arr=[]; let status='OPEN'; const n=cid.length; let ch=cash-price; //Dolar denomination--------------------------- const dolar=[0.01,0.05,0.1,0.25,1,5,10,20,100]; //--------------------------------------------- function moneda(){ for(let j=0;j<n;j++){ if(ch/dolar[j]<1){return j-1;} } } let k=(moneda()); const money={status: status,change: Money(ch,k)}; money.status=status; //----------------------------------------------------- function Money(ch,k){ for(k;k>=0;k--){ let cambio=Math.trunc(ch/dolar[k]); if(cid[k][1]==ch){status='CLOSED';arr=cid} if(cid[k][1]>(ch)){ if(cambio!=0){ arr.push([cid[k][0],cambio*dolar[k]]); ch=ch%dolar[k]; ch=Math.round(ch*100)/100;} } if(cid[k][1]<(ch)){ if(cid[k][1]!=0){ if(k==0){status='INSUFFICIENT_FUNDS';} else{ arr.push([cid[k][0],cid[k][1]]); ch=ch-cid[k][1]; ch=Math.round(ch*100)/100;} } } if(ch==0){k=0;} } return (arr); } return money; } ```

We have blurred this solution so that users who have not completed this challenge can read the discussion in this thread without giving away the solution.

We have blurred this solution so that users who have not completed this challenge can read the discussion in this thread without giving away the solution.

Using a function for the operations and a matrix for the denominations are good ideas. Is it ok if I implement these changes in my own solution?

OK, you can used a matrix of denominations of the dollar in decimals

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