I am learning Javascript at CodeAcademy.com and I am having difficulty understanding an exercise question, although I appear to have found the correct answer.
The code is designed to help someone work out how much change they should give back when someone buys something. It takes in a number and calculates how many quarters and how many pennies they should give back.
Here is what I don't understand:
• Shouldn't the code stop running the first time it encounters line 11? If not, why not?
• If the code does stop running when it enters line 11, why am I able to place code after line 10 and it will execute three times before an answer is given? I discovered that this was the case, so it made me question my understanding of how the code was working! I added the line quarters += 1; after line 10 and it returns 6.
var change = 0;
var quarters = 0;
function howManyQuarters(howMuchMoney) {
if (howMuchMoney < 0.25) {
change = howMuchMoney;
return 0;
}
else {
quarters += 1;
howManyQuarters(howMuchMoney-0.25);
return quarters; // << line 11
}
}
change = 0.99;
console.log ("Pay out " + howManyQuarters(change) + " quarters");
console.log ("And you'll have " + change * 100 + " pennies left over");
function hmQ(hwM, change) { if (hwm < 0.25) { change[0] = hwM; return 0; } else { return hwQ(hwM - 0.25) + 1; }?