I'm still a beginner, here is a complete noob, so please be patient, I went through this exercise, which basically extends the amount of penny / cent to a sufficient number of coins 25,10,5,2,1.
I tried to write only with conditional expressions, since I am still involved, therefore, I am not familiar with the built-in functions.
the function works well with checked number 65, giving an array of 25, 25, 10, 5 respectively
however, when I tested with number 46, it gave an array of 25, 25, 10, 5, 10, 10, 1, which is obviously wrong, and something clearly doesn't match my function.
Could you point out the error.
here is my code and thanks in advance.
var co = [];
function coin(n){
if (n>=25){
co.push("25");
n = n-25;
coin(n);
}
else if (n>=10){
co.push("10");
n = n-10;
coin(n);
}
else if (n>=5){
co.push("5");
n = n -5;
coin(n);
}
else if (n>=2){
co.push("2");
n = n - 2;
coin(n);
}
else if (n>=1){
co.push("1");
n = n - 1;
coin(n);
}
else if (n == 0){console.log(co);}
}