Search code examples
javascriptif-statementwhile-loopconsole.log

Whenever I get a result, it should show 2x €50 or 2x €20 instead of 1x €50 1x €50 for example. Anybody know what I should change to get this?


This code is supposed to calculate how much of each money bill I should give back based on the total change. So when the total change is €135 it should give the following result: 2x €50 1x €20 1x €10 1x €5

Instead I get the following result: 1x €50 1x €50 1x €20 1x €10 1x €5

This is probably a very easy fix but I've only been coding for a month or 2 now and I just can't seem to figure it out. The code below is what I have right now.

let wisselgeld = parseInt(process.argv[2], 10);
while (wisselgeld >= 5) {
    let aantal50 = 0;
    let aantal20 = 0;
    let aantal10 = 0;
    let aantal5 = 0;
    if (wisselgeld >= 50) {
        wisselgeld -= 50;
        aantal50++;
    } else if (wisselgeld < 50 && wisselgeld >= 20) {
        wisselgeld -= 20;
        aantal20++;
    } else if (wisselgeld < 20 && wisselgeld >= 10) {
        wisselgeld -= 10;
        aantal10++;
    } else if (wisselgeld < 10 && wisselgeld >= 5) {
        wisselgeld -= 5;
        aantal5++;
    }
    if (aantal5 > 0) {
        console.log(`${aantal5}x €5`);
    } if (aantal10 > 0) {
        console.log(`${aantal10}x €10`);
    } if (aantal20 > 0) {
        console.log(`${aantal20}x €20`);
    } if (aantal50 > 0) {
        console.log(`${aantal50}x €50`);
    }
}

I tried putting another condition in the last if statements saying it should only log to the console when the let wisselgeld is < 50 or < 20 etc. but then I get the following result: 1x €50 1x €20 1x €10 1x €5. So then I get a wrong result. Tried putting the 4 let variables outside the while loop but that didn't work either.


Solution

  • The issue with your code is that the variables aantal50, aantal20, aantal10, and aantal5 are being reset to 0 in every iteration of the while loop. This means that the count of each bill is not being accumulated correctly.

    To fix this, you should declare these variables outside of the while loop.

    let wisselgeld = parseInt(process.argv[2], 10);
    
    let aantal50 = 0;
    let aantal20 = 0;
    let aantal10 = 0;
    let aantal5 = 0;
    
    while (wisselgeld >= 5) {
        if (wisselgeld >= 50) {
            wisselgeld -= 50;
            aantal50++;
        } else if (wisselgeld < 50 && wisselgeld >= 20) {
            wisselgeld -= 20;
            aantal20++;
        } else if (wisselgeld < 20 && wisselgeld >= 10) {
            wisselgeld -= 10;
            aantal10++;
        } else if (wisselgeld < 10 && wisselgeld >= 5) {
            wisselgeld -= 5;
            aantal5++;
        }
    }
    
    if (aantal5 > 0) {
        console.log(`${aantal5}x €5`);
    } 
    if (aantal10 > 0) {
        console.log(`${aantal10}x €10`);
    } 
    if (aantal20 > 0) {
        console.log(`${aantal20}x €20`);
    } 
    if (aantal50 > 0) {
        console.log(`${aantal50}x €50`);
    }