# *JavaScript Algorithms and Data Structures Projects: Cash Register

How may I select the best cases num to get the objective. Example. I know I can make 0.50 with two quarters, But I am thinking in a way take that and make into code: I do not know what to do.

``````function checkCashRegister(price, cash, cid) {

let arrMon = [100,20,10,5,1,0.25,0.1,0.05,0.01];

let arrNamMon = ["ONE HUNDRED","TWENTY","TEN","FIVE","ONE","QUARTER", "DIME", "NICKEL", "PENNY" ];

let newArr=[];

let newObj={};

let change = cash - price;

for(let i=0;i<=cid.length;i++){

newArr.push(Math.floor(change/arrMon[i]))

}

console.log(newArr,change,arrNamMon )

}

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

dont do it like this. I did already explain, inside of here just add up the total of all the monies you have as I had suggested. I have an idea of something that might help you complete this challenge. please give me some time ill write it out for you and post it here.

1 Like

I am thankful with you, sir.

How may I set to get the names and the values in return.

``````function checkCashRegister(price, cash, cid) {

var change  = cash  - price ;

let billetes_change = [];

class billetesEnUsa{

constructor(name, value, qty){

this.name = name;

this.value = value;

this.qty = qty;

}

}

let register_bills = [

new billetesEnUsa("100 dollars", 100, cid),

new billetesEnUsa("50 dollars", 50, cid),

new billetesEnUsa("10 dollars", 10, cid),

new billetesEnUsa("5 dollars", 5, cid),

new billetesEnUsa("1 dollars", 1, cid),

new billetesEnUsa("quarter", 0.25, cid),

new billetesEnUsa("dime", 0.1, cid),

new billetesEnUsa("nickel", 0.05, cid),

new billetesEnUsa("penny", 0.01, cid),

]

register_bills.forEach(bill => {let num_billete = Math.floor(change / billetesEnUsa.value);

if (num_billete > billetesEnUsa.qty) {

num_billete = billetesEnUsa.qty;

}

if (num_billete > 0) {

billetes_change.push(new billetesEnUsa(billetesEnUsa.name, billetesEnUsa.value, num_billete))

billetesEnUsa.qty = billetesEnUsa.qty - num_billete;

change = change - (num_billete * billetesEnUsa.value);

}

});

if (change == 0) {

console.log("billetes_change", billetes_change)

console.log("register_bills", register_bills)

} else {

return {status: "INSUFFICIENT_FUNDS", change: []}

console.log("no es posible darte cambio")

}

}

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

``````
``````function  checkCashRegister(  price , cash  , cid) {

class americanCurrency  {

constructor  (name ,value  , quantity){

this.name = name  ;

this.value  = value ;

this.quantity =  quantity ;

}

}

let penny = new americanCurrency( "PENNY",  0.01, cid);

let nickel  = new   americanCurrency( "NICKEL", 0.05, cid);

let dime  = new americanCurrency  ("NICKEL" , 0.1 , cid );

let quarter  = new americanCurrency  ("QUARTER" , 0.25 , cid );

let dollar  = new americanCurrency  ("DOLLAR" , 1 , cid );

let fiveDollars  = new americanCurrency  ("FIVE" , 5 , cid );

let tenDollars  = new americanCurrency  ("TEN" , 10 , cid );

let twentyDollars  = new americanCurrency  ("TWENTY", 20 , cid );

let oneHundredDollars  = new americanCurrency  ("ONE HUNDRED", 100 , cid );

//console.log(cid)

let arrAmericanCurrency = [ oneHundredDollars ,twentyDollars  , tenDollars,fiveDollars  ,dollar , quarter,  dime, nickel, penny];

let change  =   cash  -  price  ;

let newArrAmericanCurrency=arrAmericanCurrency.map(e=>({

name: e.name,

value: e.value,

quantity:e.quantity,

numberOfNeededCurrency: Math.floor(change/e.value)

,

}))

}

How may I get the best currency to cover my change?``````

Sir, I do not understand your question, why are you returning insufficient funds? your change does not equal zero, you have enough in your drawer and in the right denominations to pay back the customer. also please understand whenver you return from a function there is no work that the function will do afterwards, no console.log no math operations, nothing. once you return it will stop doing its work any code you write afterwards will not run. in any case, please delete all that code. its no good.

please work within the following template, I give credit to @matveyka.boltach for writing the very clean code that ive used in the template :

``````var denom = [
{ name: "ONE HUNDRED", value: 100.0 },
{ name: "TWENTY", value: 20.0 },
{ name: "TEN", value: 10.0 },
{ name: "FIVE", value: 5.0 },
{ name: "ONE", value: 1.0 },
{ name: "QUARTER", value: 0.25 },
{ name: "DIME", value: 0.1 },
{ name: "NICKEL", value: 0.05 },
{ name: "PENNY", value: 0.01 },
];
function checkCashRegister(price, cash, cid) {
let change = cash - price;
//you need to return a result object with a status key and a change key,
//set that up at the top of the function to keep organized
let result = {
status: "",
change: [],
};
let sumCid = 0;
//add up all the change in the drawer to eliminate two cases the test is looking for
//A. you dont have enough to pay back the customer what you owe
//B. you have exactly enough

for (let element of cid) {
sumCid += element;
}

//case A.
if (sumCid < change) {
result.status = "INSUFFICIENT_FUNDS";
return result;
}

//case B.
if (sumCid === change) {
result.status = "CLOSED";
//you have exactly enough so you will give him everything you have,
//add it all to your change key in the result object.
//also you will of course close out your store because your register is now empty,
//as youve paid him with everything that you have.
result.change = cid;
return result;
}
//this newResult array will hold all the denomination names and their totals that your paying him back, your going to put this as the value of the change key in the object that your returning
let newResult = [];
//this is just so that your denom object matches the order you have the things inside your drawer
cid = cid.reverse();

//this is the main part of the challenge, it takes care of all cases where
//the total of everything in your drawer is not equal and not less than what you owe, as mentioned you are to use a greedy method

//you need a for loop that runs the length of all the compartments that you have in your drawer
for ( write some code in here) {
//you need a value variable that will be the amounts that you are paying back the customer of each specific denomination.

//you need a while loop that check each compartment in your register that the change you owe the customer is greater than or
//equal to the coin/denomination value, inside of the same while loop you will also have to check that you actually have coins/bills of that specific denomination in existence,
//you can not pay him back with money that does not exist
while (write some code in here) {
//inside of this while loop you basically need to do three things:
//1. subtract from what you have in your possession the coin/denomination value that you are paying out
//2. subtract from your debt to the customer (the change you owe) the coin/denomination value
//3. add the coin/denomination value to your value variable that your giving back in your object (representing the values of the denominations you are paying back to him)
}
//the conditions of your while loop are no longer true basically what this should mean to you is that your advancing to the next compartment in the register

//here you need to check that your value variable has some amount, and if it does it means your paying him something add this value to the newResult array, along with the name of the denomination i.e. dollar, quarter, dime etc....
if (write some code in here) {
//push something to the newResult array this something will be a subarray containing first the name of the denomination you are adding, and amounts that you are paying back the customer of each specific denomination.
}
}
//this means the total amount you had in your register is enough however you can not pay him back more than what you owe,
//the test is not realistic enough to consider such a case that you can give him back more
//than what you owe and he would in turn again give you back the difference. it does not work that way,
//you can ONLY return exact, else its insufficient
//hence we need the below line:
if (change > 0) {
result.status = "INSUFFICIENT_FUNDS";
return result;
}
//this below means you can successfully pay him back exact change and you still have some left over for the next customer, i.e. the next transaction that might happen
result.status = "OPEN";
result.change = newResult;
return result;
}
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],
])
);
``````

meditate on the above template for a week or so, make yourself a real cash register with monopoly money or real money if you have it, perhaps I think it may help you come to the conclusions. also if you have any further questions and no one else replies on this thread to help you, then send me a private message because freecodecamp is sending me warnings that since we are the only ones currently participating in this conversation it prefers us to move it to private chat.

1 Like

I have made a new solution. I am thankful for your answer. I apologize with the members

1 Like

I have two questions, how may solve the problem with decimals? and how can I make this a better code?

``````function checkCashRegister(price, cash, cid) {

let change  = cash  - price ;

let changeForCustomer = 0 ;

let billsAndCoinsForCustomer  = []  ;

let arrSumOfbillsAndCoinsForCustomer = [  oneHundred  , twenty  , ten ,  five , one , quarter ,  dime , nickel  , penny  ]  ;

let sumOfbillsAndCoinsForCustomer = []  ;

let oneHundred  = 0 ;

let twenty  = 0 ;

let ten = 0 ;

let five  = 0 ;

let one = 0 ;

let quarter = 0 ;

let dime  = 0 ;

let nickel  = 0 ;

let penny = 0 ;

let currency  =

[
["ONE HUNDRED", 100],
["TWENTY", 20],
["TEN", 10, ],
["FIVE", 5],
["ONE", 1],
["QUARTER", 0.25],
["DIME", 0.10],
["NICKEL", 0.05],
["PENNY", 0.01]
]

let cidCurrency  = []  ;

let avalaibleCurrency  = []  ;

for ( let i = 0 ; i < currency.length ; i++ ) {

for ( let j = 0 ; j <  cid.length ; j++ ) {

if  ( currency[i]  ==  cid[j] ){

cidCurrency.push( cid[j] )  ;

avalaibleCurrency.push( currency[i] ) ;

}

}

}

//console.log(  change  )

for ( let i = 0 ; i < avalaibleCurrency.length ;  i++ )  {

if  ( change  > avalaibleCurrency[i] )  {

while ( avalaibleCurrency[i] <= change  )  {

change  -=  avalaibleCurrency[i]  ;

changeForCustomer += avalaibleCurrency[i]  ;

billsAndCoinsForCustomer.push(  avalaibleCurrency[i] );

}

}

}

for ( let i=0 ; i < billsAndCoinsForCustomer.length ; i++ ) {

if  ( billsAndCoinsForCustomer[i]  ==  "ONE HUNDRED" )  {

oneHundred  +=   billsAndCoinsForCustomer[i] ;

} else if ( billsAndCoinsForCustomer[i]  ==  "TWENTY" ){

twenty  +=   billsAndCoinsForCustomer[i] ;

} else if ( billsAndCoinsForCustomer[i]  ==  "TEN" ){

ten  +=   billsAndCoinsForCustomer[i] ;

} else if ( billsAndCoinsForCustomer[i]  ==  "FIVE" ){

five  +=   billsAndCoinsForCustomer[i] ;

} else if ( billsAndCoinsForCustomer[i]  ==  "ONE" ){

one  +=   billsAndCoinsForCustomer[i] ;

} else if ( billsAndCoinsForCustomer[i]  ==  "QUARTER" ){

quarter  +=   billsAndCoinsForCustomer[i] ;

} else if ( billsAndCoinsForCustomer[i]  ==  "DIME" ){

dime  +=   billsAndCoinsForCustomer[i] ;

} else if ( billsAndCoinsForCustomer[i]  ==  "NICKEL" ){

niquel  +=   billsAndCoinsForCustomer[i] ;

} else  {

penny  +=   billsAndCoinsForCustomer[i] ;

}

}

//for (let  i = 0 ; i < arrSumOfbillsAndCoinsForCustomer.length  ;  i++){

if  (   oneHundred > 0 )  {

sumOfbillsAndCoinsForCustomer.push( [ "ONE HUNDRED", oneHundred ] ) ;

}

if ( twenty  > 0 )  {

sumOfbillsAndCoinsForCustomer.push( ["TWENTY", twenty] )  ;

}

if ( ten  > 0 )  {

sumOfbillsAndCoinsForCustomer.push( ["TEN", ten] ) ;

}

if ( five  > 0  )  {

sumOfbillsAndCoinsForCustomer.push( ["FIVE", five] )  ;

}

if ( one  > 0  )  {

sumOfbillsAndCoinsForCustomer.push( ["ONE", one ])  ;

}

if ( quarter  > 0  )  {

sumOfbillsAndCoinsForCustomer.push( ["QUARTER" ,quarter  ] )  ;

}

if ( dime  > 0  )  {

sumOfbillsAndCoinsForCustomer.push( ["DIME" , dime] )  ;

}
if ( nickel  > 0  )  {

sumOfbillsAndCoinsForCustomer.push( ["NICKEl", nickel  ] )  ;

}
if (  penny  > 0){

sumOfbillsAndCoinsForCustomer.push( ["PENNY", penny] )  ;

}

//}

console.log(  change  , changeForCustomer, billsAndCoinsForCustomer  )
//console.log(twenty,penny) ;

console.log( sumOfbillsAndCoinsForCustomer)
}

checkCashRegister(0.01, 100, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]]);
``````

you current code is paying the customer with monies that you do not have

1 Like

how may I substract from the CID the denominations until it is 0 if it arrives or goes to the another denomination? If I use denominationofCurrency, it works better instead of CID.

``````function checkCashRegister(price, cash, cid) {

let moneyForCustomer  = 0 ;

let change  = cash  - price ;

let quantitiesOfCashInDrawer  = cid ;

//console.log(change)

let denominationsOfCurrency =

[

["ONE HUNDRED", 100],

["TWENTY", 20],

["TEN", 10],

["FIVE", 5],

["ONE", 1],

["QUARTER", 0.25],

["DIME", 0.1],

["NICKEL", 0.05],

["PENNY", 0.01]

];

let orderedDenominationsQtyCurrency  = [ ] ;

for ( let i = 0 ; i < denominationsOfCurrency.length  ;  i++ )  {

for ( let j = 0 ; j < quantitiesOfCashInDrawer.length ; j++ ) {

if  ( denominationsOfCurrency[  i ][ 0 ] ===  quantitiesOfCashInDrawer[  j ][  0 ]  ) {

orderedDenominationsQtyCurrency.push(  quantitiesOfCashInDrawer[  j ]  )

}

}

}

for ( let i = 0 ; i < orderedDenominationsQtyCurrency.length ;  i++  ){

if  ( denominationsOfCurrency[  i ][ 1 ]  <= change  &&  quantitiesOfCashInDrawer[  i ][ 1 ] > 0  )  {

while ( denominationsOfCurrency[  i ][ 1 ]  <= change)  {

quantitiesOfCashInDrawer[  i ][ 1 ] -=  denominationsOfCurrency[  i ][ 1 ]

change  -=  quantitiesOfCashInDrawer[  i ][ 1 ] ;

moneyForCustomer  +=  quantitiesOfCashInDrawer[  i ][ 1 ] ;

}

}

}

//console.log(orderedDenominationsQtyCurrency)

console.log(change,moneyForCustomer)

}

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

quantitiesOfCashInDrawer

quantitiesOfCashInDrawer, its unnecessary, you do not need to keep repeating, use cid, javascript already has inbuilt reverse.

``````    if  ( denominationsOfCurrency[  i ][ 1 ]  <= change  &&  quantitiesOfCashInDrawer[  i ][ 1 ] > 0  )  {

while ( denominationsOfCurrency[  i ][ 1 ]  <= change)  {
``````

This is repeating. you dont need this if statement.

moneyforcustomer is currently in the wrong place. when you advance to the next change in drawer you will need to check that moneyforcustomer is greater than zero, if it is then add that to what you give back.

also in your while loop you will not decrease and add using all the money that you have in drawer compartment… but rather the value of the denomination

1 Like

I deleted that comment, because It was unclear what I was saying… was I thought you tried to make a copy of cid because maybe you had thought not to mutate cid, but you can mutate because when you make a payment it will change the original.

also you should know what you were doing
` let quantitiesOfCashInDrawer = cid ;`
would not have succeeded if that was your original intention, for that you would have had to use spread.

observe:

``````let myObj = [['penny', .01], ['dime', .1]]

function Mutate(myObj) {
let newObj = myObj
newObj.splice(1, 0, 'x');
}
Mutate(myObj)

console.log(myObj, "=myObj")

let anotherObj = [['quarter', .25], ['dollar', 1]]

function Pure(myObj) {
let newObj = [...myObj]
newObj.splice(1, 0, 'x');
}
Pure(anotherObj)

console.log(anotherObj, "=anotherObj")
``````

and also spread will only work for that purpose if its not nested…

1 Like

How may I solve the problems with decimals? how May ,make this a better code?

``````function checkCashRegister(price, cash, cid) {

let moneyForCustomer  = 0 ;

let change  = cash  - price ;

let oneHundred  = 0 ;

let twenty  = 0 ;

let ten = 0 ;

let five  = 0 ;

let one = 0 ;

let quarter = 0 ;

let dime  = 0 ;

let nickel  = 0 ;

let penny = 0 ;

let billsAnDCoinsForCustomer  = [ ] ;

let newCid  = [ ] ;

let newDenominationsofCurrency  =  [ ]  ;

let totalForCustomer  =  [ ]  ;

let denominationsOfCurrency =

[

["ONE HUNDRED", 100],

["TWENTY", 20],

["TEN", 10],

["FIVE", 5],

["ONE", 1],

["QUARTER", 0.25],

["DIME", 0.1],

["NICKEL", 0.05],

["PENNY", 0.01]

];

for ( let i = 0 ; i < denominationsOfCurrency.length  ;  i++ )  {

for ( let j = 0 ; j < cid.length ; j++ ) {

if  ( denominationsOfCurrency[  i ][ 0 ] ===  cid[  j ][  0 ]  ) {

newCid.push(  cid[  j ]  ) ;

newDenominationsofCurrency.push(  denominationsOfCurrency[  i ] )

}

}

}

for(  let i = 0;  i < denominationsOfCurrency.length ; i ++ )  {

while (change > denominationsOfCurrency[ i ][  1 ]   && newCid [ i ][  1 ]  > 0   )  {

change  -=  denominationsOfCurrency[ i ][  1 ]  ;

newCid [ i ][  1 ]  -=  denominationsOfCurrency[ i ][  1 ]  ;

moneyForCustomer  +=  denominationsOfCurrency[ i ][  1 ]  ;

billsAnDCoinsForCustomer.push(  denominationsOfCurrency[ i ]  )

}

}

for ( let i = 0 ; i < billsAnDCoinsForCustomer.length ; i++  )  {

if (  billsAnDCoinsForCustomer[ i ][  0 ] == "ONE HUNDRED"  ){

oneHundred  +=  100 ;

}

if (  billsAnDCoinsForCustomer[ i ][  0 ] == "TWENTY"  ){

twenty  +=  20 ;

}

if (  billsAnDCoinsForCustomer[ i ][  0 ] == "TEN"  ){

ten  +=  10 ;

}

if (  billsAnDCoinsForCustomer[ i ][  0 ] == "FIVE"  ){

five  +=  5 ;

}

if (  billsAnDCoinsForCustomer[ i ][  0 ] == "ONE"  ){

one  +=  1 ;

}

if (  billsAnDCoinsForCustomer[ i ][  0 ] == "QUARTER"  ){

quarter  +=  0.25 ;

}

if (  billsAnDCoinsForCustomer[ i ][  0 ] == "DIME"  ){

dime  +=  0.10 ;

}

if (  billsAnDCoinsForCustomer[ i ][  0 ] == "NICKEL"  ){

nickel  +=  0.05 ;

}

if (  billsAnDCoinsForCustomer[ i ][  0 ] == "PENNY"  ){

penny  +=  0.01 ;

}

}

if  ( oneHundred  > 0 ) {

totalForCustomer.push(  ["ONE HUNDRED", oneHundred ])

}

if ( twenty  > 0 )  {

totalForCustomer.push(  ["TWENTY", twenty ])

}

if ( ten  > 0 )  {

totalForCustomer.push(  ["TEN", ten ])

}

if ( five  > 0 )  {

totalForCustomer.push(  ["FIVE", five ])

}

if ( one  > 0 )  {

totalForCustomer.push(  ["ONE", one ])

}

if ( quarter  > 0 )  {

totalForCustomer.push(  ["QUARTER", quarter ])

}

if ( dime  > 0 )  {

totalForCustomer.push(  ["DIME", dime ])

}

if ( nickel  > 0 )  {

totalForCustomer.push(  ["NICKEL", nickel ])

}

if ( penny  > 0 )  {

totalForCustomer.push(  ["PENNY", penny ])

}

return  totalForCustomer

}

checkCashRegister(0.01, 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 think that I have done it.

``````function checkCashRegister(price, cash, cid) {

let mutatedCID  = [ ]

let moneyForCustomer  = 0 ;

let change  = cash*1000  - price*1000 ;

let oneHundred  = 0 ;

let twenty  = 0 ;

let ten = 0 ;

let five  = 0 ;

let one = 0 ;

let quarter = 0 ;

let dime  = 0 ;

let nickel  = 0 ;

let penny = 0 ;

let billsAnDCoinsForCustomer  = [ ] ;

let newCid  = [ ] ;

let newDenominationsofCurrency  =  [ ]  ;

let totalForCustomer  =  [ ]  ;

let denominationsOfCurrency =

[

["ONE HUNDRED", 100000],

["TWENTY", 20000],

["TEN", 10000],

["FIVE", 5000],

["ONE", 1000],

["QUARTER", 250],

["DIME", 100],

["NICKEL", 50],

["PENNY", 10]

];

for ( let i = 0 ; i < denominationsOfCurrency.length  ;  i++ )  {

for ( let j = 0 ; j < cid.length ; j++ ) {

if  ( denominationsOfCurrency[  i ][ 0 ] ===  cid[  j ][  0 ]  ) {

newCid.push(  cid[  j  ]  ) ;

newDenominationsofCurrency.push(  denominationsOfCurrency[  i  ] )

}

}

}

for (   let i = 0 ; i < newCid.length ; i++  )  {

mutatedCID.push(newCid[  i ][ 1 ] * 1000)

}

for(  let i = 0;  i < denominationsOfCurrency.length ; i ++ )  {

while (change >= denominationsOfCurrency[ i ][  1 ]   && mutatedCID [ i ]  > 0   )  {

change  -=  denominationsOfCurrency[ i ][  1 ]  ;

mutatedCID [ i ]  -=  denominationsOfCurrency[ i ][  1 ]  ;

moneyForCustomer  +=  denominationsOfCurrency[ i ][  1 ]  ;

billsAnDCoinsForCustomer.push(  denominationsOfCurrency[ i ]  )

}

}

for ( let i = 0 ; i < billsAnDCoinsForCustomer.length ; i++  )  {

if (  billsAnDCoinsForCustomer[ i ][  0 ] == "ONE HUNDRED"  ){

oneHundred  +=  100000 ;

}

if (  billsAnDCoinsForCustomer[ i ][  0 ] == "TWENTY"  ){

twenty  +=  20000 ;

}

if (  billsAnDCoinsForCustomer[ i ][  0 ] == "TEN"  ){

ten  +=  10000 ;

}

if (  billsAnDCoinsForCustomer[ i ][  0 ] == "FIVE"  ){

five  +=  5000 ;

}

if (  billsAnDCoinsForCustomer[ i ][  0 ] == "ONE"  ){

one  +=  1000 ;

}

if (  billsAnDCoinsForCustomer[ i ][  0 ] == "QUARTER"  ){

quarter  +=  250 ;

}

if (  billsAnDCoinsForCustomer[ i ][  0 ] == "DIME"  ){

dime  +=  100 ;

}

if (  billsAnDCoinsForCustomer[ i ][  0 ] == "NICKEL"  ){

nickel  +=  50 ;

}

if (  billsAnDCoinsForCustomer[ i ][  0 ] == "PENNY"  ){

penny  +=  10 ;

}

}

if  ( oneHundred  > 0 ) {

totalForCustomer.push(  ["ONE HUNDRED", oneHundred/1000 ])

}

if ( twenty  > 0 )  {

totalForCustomer.push(  ["TWENTY", twenty/1000 ])

}

if ( ten  > 0 )  {

totalForCustomer.push(  ["TEN", ten/1000 ])

}

if ( five  > 0 )  {

totalForCustomer.push(  ["FIVE", five/1000 ])

}

if ( one  > 0 )  {

totalForCustomer.push(  ["ONE", one/1000 ])

}

if ( quarter  > 0 )  {

totalForCustomer.push(  ["QUARTER", quarter/1000 ])

}

if ( dime  > 0 )  {

totalForCustomer.push(  ["DIME", dime/1000 ])

}

if ( nickel  > 0 )  {

totalForCustomer.push(  ["NICKEL", nickel/1000 ])

}

if ( penny  > 0 )  {

totalForCustomer.push(  ["PENNY", penny/1000 ])

}

console.log(totalForCustomer)
return  totalForCustomer

}

checkCashRegister(50, 100, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]]);``````

it works… well sort of, but the code it can be better, some things I can tell you:

1. all of this you are doing multiplying by 1000 and dividing by 1000 rather use `toFixed(2)`
like the value of a penny isnt really 10… I mean its much clearer to think of it as having the value of .01 and likewise one hundred should have the value of 100…
1. you arent returning it like the test wants it you need to return an object with a status key and a change key the change being your totalForCustomer variable (in an array)

2. why are you doing this?

``````    for (let i = 0; i < denominationsOfCurrency.length; i++) {

for (let j = 0; j < cid.length; j++) {

if (denominationsOfCurrency[i] === cid[j]) {

newCid.push(cid[j]);

newDenominationsofCurrency.push(denominationsOfCurrency[i])

}

}

}
``````
1. you not handling a case of where you dont have enough funds to make change or where the total of your funds is more than the change due but you dont have the right denominations in your drawer to make exact change (still insufficient funds). for the first case would be at the top of your function just add up everything you have youll know right away…
1 Like
1. in a case like:
``````checkCashRegister(19.5, 20, [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]])
``````

you owe him .50 cents youll not return him [ ‘PENNY’, 0.01 ] rather youll return him an empty array and status insufficient funds…

the reason you are doing this is because of this line:

its in the incorrect place, what i mean is that you should only be pushing stuff in there after the while loop and only in a certain case… put the moneyForCustomer variable inside the forloop but above the while loop. that way youll just reset it to zero when you advance to the next compartment and if there is anything in it, than push it.

instead of what you did you said in your while loop that the change you owed was greater than the value of denom and you had some money so you just paid it anyways, even though you didnt have enough of it to make the change.

1. style- can you please make the code not spaced out like instead of this:
``````someFunctiontooMuchSpaced(){

I am a line of code

I am a second line of code

I am a third line lorum ipsum..

}
``````

it would look rather like this:

``````correctlySpaced(){
I am a line of code
I am a second line of code
I am a third line lorum ipsum..
}
``````

but you can have a space between your functions themselves that is fine.

1 Like

Hello!. It started to work like the problem asked for, but the case `checkCashRegister(19.5, 20, [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 1], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]])` should return `{status: "INSUFFICIENT_FUNDS", change: []}`It is not working. I think that is a bug. I correct a little of the spaces you have told me. I will begin to follow the tips you gave me. The lines of code that you asked for them are for setting the array in order from the highest one to the lowest one.

``````
function checkCashRegister(price, cash, cid) {

let mutatedCID  = [ ]

let moneyForCustomer  = 0 ;

let change  = cash*1000  - price*1000 ;

let oneHundred  = 0 ;

let twenty  = 0 ;

let ten = 0 ;

let five  = 0 ;

let one = 0 ;

let quarter = 0 ;

let dime  = 0 ;

let nickel  = 0 ;

let penny = 0 ;

let billsAnDCoinsForCustomer  = [ ] ;

let newCid  = [ ] ;

let newDenominationsofCurrency  =  [ ]  ;

let totalForCustomer  =  [ ]  ;

let backwardMutatedCID  = [ ] ;

const reducer = (accumulator, currentValue) => accumulator + currentValue;

let obj1 = {

status: "OPEN"  ,

change: totalForCustomer

}

let obj2 = {

status: "INSUFFICIENT_FUNDS"  ,

change: []

}

let obj3  =  {

status: "CLOSED"  ,

change: backwardMutatedCID

}

let denominationsOfCurrency =

[

["ONE HUNDRED", 100000],

["TWENTY", 20000],

["TEN", 10000],

["FIVE", 5000],

["ONE", 1000],

["QUARTER", 250],

["DIME", 100],

["NICKEL", 50],

["PENNY", 10]

];

for ( let i = 0 ; i < denominationsOfCurrency.length  ;  i++ )  {

for ( let j = 0 ; j < cid.length ; j++ ) {

if  ( denominationsOfCurrency[  i ][ 0 ] ===  cid[  j ][  0 ]  ) {

newCid.push(  cid[  j  ]  ) ;

newDenominationsofCurrency.push(  denominationsOfCurrency[  i  ] )

}

}

}

for (   let i = 0 ; i < newCid.length ; i++  )  {

mutatedCID.push(newCid[  i ][ 1 ] * 1000)

}

let totalInMutatedCID  = mutatedCID.reduce(reducer);

//console.log(totalInMutatedCID,change)

if( totalInMutatedCID  > change ){

for(  let i = 0;  i < denominationsOfCurrency.length ; i ++ )  {

while (change >= denominationsOfCurrency[ i ][  1 ]   && mutatedCID [ i ]  > 0   )  {

change  -=  denominationsOfCurrency[ i ][  1 ]  ;

mutatedCID [ i ]  -=  denominationsOfCurrency[ i ][  1 ]  ;

moneyForCustomer  +=  denominationsOfCurrency[ i ][  1 ]  ;

billsAnDCoinsForCustomer.push(  denominationsOfCurrency[ i ]  )

}

}

for ( let i = 0 ; i < billsAnDCoinsForCustomer.length ; i++  )  {

if (  billsAnDCoinsForCustomer[ i ][  0 ] == "ONE HUNDRED"  ){

oneHundred  +=  100000 ;

}

if (  billsAnDCoinsForCustomer[ i ][  0 ] == "TWENTY"  ){

twenty  +=  20000 ;

}

if (  billsAnDCoinsForCustomer[ i ][  0 ] == "TEN"  ){

ten  +=  10000 ;

}

if (  billsAnDCoinsForCustomer[ i ][  0 ] == "FIVE"  ){

five  +=  5000 ;

}

if (  billsAnDCoinsForCustomer[ i ][  0 ] == "ONE"  ){

one  +=  1000 ;

}

if (  billsAnDCoinsForCustomer[ i ][  0 ] == "QUARTER"  ){

quarter  +=  250 ;

}

if (  billsAnDCoinsForCustomer[ i ][  0 ] == "DIME"  ){

dime  +=  100 ;

}

if (  billsAnDCoinsForCustomer[ i ][  0 ] == "NICKEL"  ){

nickel  +=  50 ;

}

if (  billsAnDCoinsForCustomer[ i ][  0 ] == "PENNY"  ){

penny  +=  10 ;

}

}

if  ( oneHundred  > 0 ) {

totalForCustomer.push(  ["ONE HUNDRED", oneHundred/1000 ])

}

if ( twenty  > 0 )  {

totalForCustomer.push(  ["TWENTY", twenty/1000 ])

}

if ( ten  > 0 )  {

totalForCustomer.push(  ["TEN", ten/1000 ])

}

if ( five  > 0 )  {

totalForCustomer.push(  ["FIVE", five/1000 ])

}

if ( one  > 0 )  {

totalForCustomer.push(  ["ONE", one/1000 ])

}

if ( quarter  > 0 )  {

totalForCustomer.push(  ["QUARTER", quarter/1000 ])

}

if ( dime  > 0 )  {

totalForCustomer.push(  ["DIME", dime/1000 ])

}

if ( nickel  > 0 )  {

totalForCustomer.push(  ["NICKEL", nickel/1000 ])

}

if ( penny  > 0 )  {

totalForCustomer.push(  ["PENNY", penny/1000 ])

}

return obj1

} else if (totalInMutatedCID  < change ){

return obj2

} else  {

for(  let i = 0;  i < denominationsOfCurrency.length ; i ++ )  {

while (change >= denominationsOfCurrency[ i ][  1 ]   && mutatedCID [ i ]  > 0   )  {

change  -=  denominationsOfCurrency[ i ][  1 ]  ;

mutatedCID [ i ]  -=  denominationsOfCurrency[ i ][  1 ]  ;

moneyForCustomer  +=  denominationsOfCurrency[ i ][  1 ]  ;

billsAnDCoinsForCustomer.push(  denominationsOfCurrency[ i ]  )

}

}

for ( let i = 0 ; i < billsAnDCoinsForCustomer.length ; i++  )  {

if (  billsAnDCoinsForCustomer[ i ][  0 ] == "ONE HUNDRED"  ){

oneHundred  +=  100000 ;

}

if (  billsAnDCoinsForCustomer[ i ][  0 ] == "TWENTY"  ){

twenty  +=  20000 ;

}

if (  billsAnDCoinsForCustomer[ i ][  0 ] == "TEN"  ){

ten  +=  10000 ;

}

if (  billsAnDCoinsForCustomer[ i ][  0 ] == "FIVE"  ){

five  +=  5000 ;

}

if (  billsAnDCoinsForCustomer[ i ][  0 ] == "ONE"  ){

one  +=  1000 ;

}

if (  billsAnDCoinsForCustomer[ i ][  0 ] == "QUARTER"  ){

quarter  +=  250 ;

}

if (  billsAnDCoinsForCustomer[ i ][  0 ] == "DIME"  ){

dime  +=  100 ;

}

if (  billsAnDCoinsForCustomer[ i ][  0 ] == "NICKEL"  ){

nickel  +=  50 ;

}

if (  billsAnDCoinsForCustomer[ i ][  0 ] == "PENNY"  ){

penny  +=  10 ;

}

}

if  ( oneHundred  => 0 ) {

totalForCustomer.push(  ["ONE HUNDRED", oneHundred/1000 ])

}

if ( twenty  => 0 )  {

totalForCustomer.push(  ["TWENTY", twenty/1000 ])

}

if ( ten  => 0 )  {

totalForCustomer.push(  ["TEN", ten/1000 ])

}

if ( five  => 0 )  {

totalForCustomer.push(  ["FIVE", five/1000 ])

}

if ( one  => 0 )  {

totalForCustomer.push(  ["ONE", one/1000 ])

}

if ( quarter  => 0 )  {

totalForCustomer.push(  ["QUARTER", quarter/1000 ])

}

if ( dime  => 0 )  {

totalForCustomer.push(  ["DIME", dime/1000 ])

}

if ( nickel  => 0 )  {

totalForCustomer.push(  ["NICKEL", nickel/1000 ])

}

if ( penny  => 0 )  {

totalForCustomer.push(  ["PENNY", penny/1000 ])

}

for(    let i   =   0   ;   i   <   cid.length  ;  i++ ){

for(    let j   =   0   ;   j   <   totalForCustomer.length  ;   j++ )   {

if( totalForCustomer[i]  ==  cid[j]  ){

backwardMutatedCID.push(totalForCustomer[j])

}

}

}

return obj3

}

}``````

Thanks @kravmaguy @miku86 @ajborla @gutsytechster for accompanying me in this journey. It is done! I would like to optimize it because there is too much hard code here, I know I can improve it.

``````function checkCashRegister(price, cash, cid) {

let mutatedCID  = [ ]
let moneyForCustomer  = 0 ;
let change  = cash*1000  - price*1000 ;
let oneHundred  = 0 ;
let twenty  = 0 ;
let ten = 0 ;
let five  = 0 ;
let one = 0 ;
let quarter = 0 ;
let dime  = 0 ;
let nickel  = 0 ;
let penny = 0 ;
let billsAnDCoinsForCustomer  = [ ] ;
let newCid  = [ ] ;
let newDenominationsofCurrency  =  [ ]  ;
let totalForCustomer  =  [ ]  ;
let backwardMutatedCID  = [ ] ;
const reducer = (accumulator, currentValue) => accumulator + currentValue;

let obj1 = {
status: "OPEN"  ,
change: totalForCustomer

}

let obj2  =  {
status: "CLOSED"  ,
change: backwardMutatedCID

}

let obj3 = {
status: "INSUFFICIENT_FUNDS"  ,
change: []

}
let denominationsOfCurrency =
[

["ONE HUNDRED", 100000],

["TWENTY", 20000],

["TEN", 10000],

["FIVE", 5000],

["ONE", 1000],

["QUARTER", 250],

["DIME", 100],

["NICKEL", 50],

["PENNY", 10]
];

for ( let i = 0 ; i < denominationsOfCurrency.length  ;  i++ )  {
for ( let j = 0 ; j < cid.length ; j++ ) {
if  ( denominationsOfCurrency[  i ][ 0 ] ===  cid[  j ][  0 ]  ) {
newCid.push(  cid[  j  ]  ) ;
newDenominationsofCurrency.push(  denominationsOfCurrency[  i  ] )

}
}
}

for (   let i = 0 ; i < newCid.length ; i++  )  {
mutatedCID.push(newCid[  i ][ 1 ] * 1000)
}

let totalInMutatedCID  = mutatedCID.reduce(reducer);
//console.log(totalInMutatedCID,change)
//console.log(mutatedCID)
if( totalInMutatedCID  > change   &&  change<=mutatedCID ){
for(  let i = 0;  i < denominationsOfCurrency.length ; i ++ )  {
while (change >= denominationsOfCurrency[ i ][  1 ]   && mutatedCID [ i ]  > 0   )  {
change  -=  denominationsOfCurrency[ i ][  1 ]  ;
mutatedCID [ i ]  -=  denominationsOfCurrency[ i ][  1 ]  ;
moneyForCustomer  +=  denominationsOfCurrency[ i ][  1 ]  ;
billsAnDCoinsForCustomer.push(  denominationsOfCurrency[ i ]  )

}
}
for ( let i = 0 ; i < billsAnDCoinsForCustomer.length ; i++  )  {
if (  billsAnDCoinsForCustomer[ i ][  0 ] == "ONE HUNDRED"  ){
oneHundred  +=  100000 ;
}
if (  billsAnDCoinsForCustomer[ i ][  0 ] == "TWENTY"  ){
twenty  +=  20000 ;
}
if (  billsAnDCoinsForCustomer[ i ][  0 ] == "TEN"  ){
ten  +=  10000 ;
}
if (  billsAnDCoinsForCustomer[ i ][  0 ] == "FIVE"  ){
five  +=  5000 ;
}
if (  billsAnDCoinsForCustomer[ i ][  0 ] == "ONE"  ){
one  +=  1000 ;
}
if (  billsAnDCoinsForCustomer[ i ][  0 ] == "QUARTER"  ){
quarter  +=  250 ;
}
if (  billsAnDCoinsForCustomer[ i ][  0 ] == "DIME"  ){
dime  +=  100 ;
}
if (  billsAnDCoinsForCustomer[ i ][  0 ] == "NICKEL"  ){
nickel  +=  50 ;
}
if (  billsAnDCoinsForCustomer[ i ][  0 ] == "PENNY"  ){
penny  +=  10 ;
}

}

if  ( oneHundred  > 0 ) {
totalForCustomer.push(  ["ONE HUNDRED", oneHundred/1000 ])
}
if ( twenty  > 0 )  {
totalForCustomer.push(  ["TWENTY", twenty/1000 ])
}
if ( ten  > 0 )  {
totalForCustomer.push(  ["TEN", ten/1000 ])
}
if ( five  > 0 )  {
totalForCustomer.push(  ["FIVE", five/1000 ])
}
if ( one  > 0 )  {
totalForCustomer.push(  ["ONE", one/1000 ])
}
if ( quarter  > 0 )  {
totalForCustomer.push(  ["QUARTER", quarter/1000 ])
}
if ( dime  > 0 )  {
totalForCustomer.push(  ["DIME", dime/1000 ])
}
if ( nickel  > 0 )  {
totalForCustomer.push(  ["NICKEL", nickel/1000 ])
}
if ( penny  > 0 )  {
totalForCustomer.push(  ["PENNY", penny/1000 ])
}

return obj1

} else if (totalInMutatedCID  == change  ){

for(  let i = 0;  i < denominationsOfCurrency.length ; i ++ )  {
while (change >= denominationsOfCurrency[ i ][  1 ]   && mutatedCID [ i ]  > 0   )  {
change  -=  denominationsOfCurrency[ i ][  1 ]  ;
mutatedCID [ i ]  -=  denominationsOfCurrency[ i ][  1 ]  ;
moneyForCustomer  +=  denominationsOfCurrency[ i ][  1 ]  ;
billsAnDCoinsForCustomer.push(  denominationsOfCurrency[ i ]  )

}
}

for ( let i = 0 ; i < billsAnDCoinsForCustomer.length ; i++  )  {

if (  billsAnDCoinsForCustomer[ i ][  0 ] == "ONE HUNDRED"  ){
oneHundred  +=  100000 ;
}
if (  billsAnDCoinsForCustomer[ i ][  0 ] == "TWENTY"  ){
twenty  +=  20000 ;
}
if (  billsAnDCoinsForCustomer[ i ][  0 ] == "TEN"  ){
ten  +=  10000 ;
}
if (  billsAnDCoinsForCustomer[ i ][  0 ] == "FIVE"  ){
five  +=  5000 ;
}
if (  billsAnDCoinsForCustomer[ i ][  0 ] == "ONE"  ){
one  +=  1000 ;
}
if (  billsAnDCoinsForCustomer[ i ][  0 ] == "QUARTER"  ){
quarter  +=  250 ;
}
if (  billsAnDCoinsForCustomer[ i ][  0 ] == "DIME"  ){
dime  +=  100 ;
}
if (  billsAnDCoinsForCustomer[ i ][  0 ] == "NICKEL"  ){
nickel  +=  50 ;
}
if (  billsAnDCoinsForCustomer[ i ][  0 ] == "PENNY"  ){
penny  +=  10 ;
}

}

if  ( oneHundred  => 0 ) {
totalForCustomer.push(  ["ONE HUNDRED", oneHundred/1000 ])
}
if ( twenty  => 0 )  {
totalForCustomer.push(  ["TWENTY", twenty/1000 ])
}
if ( ten  => 0 )  {
totalForCustomer.push(  ["TEN", ten/1000 ])
}
if ( five  => 0 )  {
totalForCustomer.push(  ["FIVE", five/1000 ])
}
if ( one  => 0 )  {
totalForCustomer.push(  ["ONE", one/1000 ])
}
if ( quarter  => 0 )  {
totalForCustomer.push(  ["QUARTER", quarter/1000 ])
}
if ( dime  => 0 )  {
totalForCustomer.push(  ["DIME", dime/1000 ])
}
if ( nickel  => 0 )  {
totalForCustomer.push(  ["NICKEL", nickel/1000 ])
}
if ( penny  => 0 )  {
totalForCustomer.push(  ["PENNY", penny/1000 ])
}

for(    let i   =   0   ;   i   <   cid.length  ;  i++ ){

for(    let j   =   0   ;   j   <   totalForCustomer.length  ;   j++ )   {

if( totalForCustomer[i]  ==  cid[j]  ){

backwardMutatedCID.push(totalForCustomer[j])

}

}

}

return obj2

} else  {

return obj3

}

}
let a = checkCashRegister(19.5, 20, [["PENNY", 0], ["NICKEL", 0], ["DIME",0], ["QUARTER", 0], ["ONE", 1], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]);

console.log(a)``````

negative. it does not work.

1 Like