Hello, can anyone tell me why in tutorials this is used:
instead of what I was thinking:
My method seems much more efficient...is there a reason to use recursion over a small while loop?
(Please note this is only a piece from an object, so it is not complete.)
Code:
howManyDollars: function(amountOfMoney) {
if(amountOfMoney < 1){
return 0;
}
else {
return 1.00+this.howManyDollars(amountOfMoney-1);
}
}
instead of what I was thinking:
Code:
howManyDollars: function(amountOfMoney) {
while(amountOfMoney!<1){
return 1.00+(this.howManyDollars(amountOfMoney--);
}
return 0;
}
My method seems much more efficient...is there a reason to use recursion over a small while loop?
(Please note this is only a piece from an object, so it is not complete.)







