The cashier problem

can someone help me with this problem? I don’t understand why the code doesn’t work.
*# The Cashier Problem

Create a program that helps a cashier give adequate change to customers. The program should return the amount of notes and coins for the customer’s change.

Example: If the price is €3.75 and the paid amount is €50, then the client should receive €46.25 back in change.

  • The expected output should be:

  • 2 x €20 // 2 twenty euro notes

  • 1 x €5 // 1 five euro note

  • 1 x €1 // 1 euro

  • 1 x €0.2 // 1 twenty cent coin

  • 1 x €0.05 // 1 five cent coin

Example: Price: €4.50, Paid amount: €20, Change: 15.50

  • Expected output:

  • 1 x €10

  • 1 x €5

  • 1 x €0.5

  • Notes

  • Include outputs for exceptions e.g. price: €4, paid amount: €3.

*/

const cashier=(price, cash)=>{
    let amountOfNotes;
    let change = parseFloat((cash - price).toFixed(2));
    console.log(`Price: ${price}`);
    console.log(`Amount:${cash}`);
    console.log(`Change:${change}`);
    

    let current=[
        ["one cent coin",.01],
        ["two cents coin",.02],
        ["five cents coin",.05],
        ["ten cents coin",.10],
        ["twenty cents coin",.20],
        ["fifty cents coin",.50],
        ["one euro coin",1],
        ["two euros coin",2],
        ["five euros notes",5],
        ["ten euros notes",10],
        ["twenty euros notes",20],
        ["fifty euros notes",50],
        ["hundred euros notes",100],
        ["two hundred euros notes",200],
        ["five hundred euros notes",500]
    
    ]

        for(let i=0;i<current.length;i++){
        current.reverse();
        console.log(current);
        if(change>=current[i][1]){
            amountOfNotes=Math.floor(change/current[i][1]);
            console.log(amountOfNotes)
            change=parseFloat((change%current[i][1]).toFixed(2));
            console.log(`${amountOfNotes} x ${current[i][1]} // ${amountOfNotes}x ${current[i][0]}`)
        }
    } 

    
}
cashier(3.25,100);

Hi! Can someone help me with this problem? I don’t understand why the code doesn’t work…Thanks.

The Cashier Problem

Create a program that helps a cashier give adequate change to customers. The program should return the amount of notes and coins for the customer’s change.

Example: If the price is €3.75 and the paid amount is €50, then the client should receive €46.25 back in change.

  • The expected output should be:

  • 2 x €20 // 2 twenty euro notes

  • 1 x €5 // 1 five euro note

  • 1 x €1 // 1 euro

  • 1 x €0.2 // 1 twenty cent coin

  • 1 x €0.05 // 1 five cent coin

Example: Price: €4.50, Paid amount: €20, Change: 15.50

  • Expected output:

  • 1 x €10

  • 1 x €5

  • 1 x €0.5

  • Notes

  • Include outputs for exceptions e.g. price: €4, paid amount: €3.

*/

const cashier=(price, cash)=>{
    let amountOfNotes;
    let change = parseFloat((cash - price).toFixed(2));
    console.log(`Price: ${price}`);
    console.log(`Amount:${cash}`);
    console.log(`Change:${change}`);
    

    let current=[
        ["one cent coin",.01],
        ["two cents coin",.02],
        ["five cents coin",.05],
        ["ten cents coin",.10],
        ["twenty cents coin",.20],
        ["fifty cents coin",.50],
        ["one euro coin",1],
        ["two euros coin",2],
        ["five euros notes",5],
        ["ten euros notes",10],
        ["twenty euros notes",20],
        ["fifty euros notes",50],
        ["hundred euros notes",100],
        ["two hundred euros notes",200],
        ["five hundred euros notes",500]
    
    ]

        for(let i=0;i<current.length;i++){
        current.reverse();
        console.log(current);
        if(change>=current[i][1]){
            amountOfNotes=Math.floor(change/current[i][1]);
            console.log(amountOfNotes)
            change=parseFloat((change%current[i][1]).toFixed(2));
            console.log(`${amountOfNotes} x ${current[i][1]} // ${amountOfNotes}x ${current[i][0]}`)
        }
    } 

    
}
cashier(3.25,100);

You already put a console into your loop, so you must have noticed, that you toggle the order of the current array 15 times. You only need to do that once. So just place that outside of the loop.

Thanks :wink:

const cashier=(price, cash)=>{

    let amountOfNotes;
    let change = parseFloat((cash - price).toFixed(2));
    console.log(`Price: €${price}`);
    console.log(`Amount:€${cash}`);
    console.log(`Change:€${change}`);
    

    let current=[
        ["one cent coin",.01],
        ["two cents coin",.02],
        ["five cents coin",.05],
        ["ten cents coin",.10],
        ["twenty cents coin",.20],
        ["fifty cents coin",.50],
        ["one euro coin",1.00],
        ["two euros coin",2.00],
        ["five euros notes",5.00],
        ["ten euros notes",10.00],
        ["twenty euros notes",20.00],
        ["fifty euros notes",50.00],
        ["hundred euros notes",100.00],
        ["two hundred euros notes",200.00],
        ["five hundred euros notes",500.00]
    
    ]
    current.reverse(); 
    for(let i=0;i<current.length;i++){
        
        if(change>=current[i][1]){
            
            amountOfNotes=Math.floor(change/current[i][1]);
            change=parseFloat((change%current[i][1]).toFixed(2));
            console.log(`${amountOfNotes} x €${current[i][1]} // ${amountOfNotes} x ${current[i][0]}`)
        }
    }   
}
cashier(3.75,50.00);
/*
Price: €3.75
Amount:€50
Change:€46.25
2 x €20 // 2x twenty euros notes
1 x €5 // 1x five euros notes
1 x €1 // 1x one euro coin
1 x €0.2 // 1x twenty cents coin
1 x €0.05 // 1x five cents coin*/
cashier(4.50,20.00);
/*
Price: €4.5
Amount:€20
Change:€15.5
1 x €10 // 1x ten euros notes
1 x €5 // 1x five euros notes
1 x €0.5 // 1x fifty cents coin
*/