Trying to write a program that returns the exact change in coins the coins am using are [1, 0.50, 0.20, 0.10, 0.05, 0.02, 0.01]. am trying to do it using only conditional statements. my problem is that the last "IF" statement isn't working as i want it to, and i dont get what is wrong.
for example for the current number the outcome should be [1 x 0.20 and 2 x 0.02]. in place of that it is returning me [1 x 0,20 and 1 x 0.02 and 1 x 0.01]
var price = 0.76;
var paid = 1;
var a = paid - price;
var oneLeva = 0;
var fiftyCents = 0;
var twentyCents = 0;
var tenCents = 0;
var fiveCents = 0;
var twoCents = 0;
var oneCents = 0;
if((a - 1) >= 0){
oneLeva += 1;
a -= 1;
if((a - 1) >= 0){
oneLeva += 1;
a -= 1;
if((a - 1) >= 0){
oneLeva += 1;
a -= 1;
if((a - 1) >= 0){
oneLeva += 1;
a -= 1;
}
}
}
}
if((a - 0.50) >= 0){
fiftyCents += 1;
a -= 0.50;
}
if((a - 0.20) >= 0){
twentyCents += 1;
a -= 0.20;
if((a - 0.20) >= 0){
twentyCents += 1;
a -= 0.20;
}
}
if((a - 0.10) >= 0){
tenCents += 1;
a -= 0.10;
}
if((a - 0.05) >= 0){
fiveCents += 1;
a -= 0.05;
}
if((a - 0.02) >= 0){
twoCents += 1;
a -= 0.02;
if((a - 0.02) >= 0){
twoCents += 1;
a -= 0.02;
}
}
if((a - 0.01) > 0){
oneCents += 1;
a -= 0.01;
}
console.log(oneLeva);
console.log(fiftyCents);
console.log(twentyCents);
console.log(tenCents);
console.log(fiveCents);
console.log(twoCents);
console.log(oneCents);