1.5 RangeError: Maximum call stack size exceeded.
Don’t know why my code is returning this, but here it is:
function howManyCoins (coinName, coinAmount, coinsSoFar) {
if (change < coinAmount) {
console.log(coinsSoFar + " " + coinName);
} else { change -= coinAmount;
return howManyCoins(coinsSoFar + 1);
}
}
change = 4.94;
console.log("Give them:");
howManyCoins("dollar bills", 1.00, 0);
howManyCoins("quarters", 0.25, 0);
howManyCoins("dimes", 0.10, 0);
howManyCoins("nickels", 0.05, 0);
howManyCoins("pennies", 0.01, 0);
console.log("And the amount of change left to give should be $0.00. It actually is $" + change.toFixed(2));
I think it’s right too, since it’s pretty much a replica of the previous exercise.
Edit: I’m also using Chrome and went to Firefox to check if it was my browser and the syntax said:
InternalError: Too much recursion.
I had to use the labs window, because it wouldn’t load the text editor for the project window.
Anyways, I checked on both Firefox and Chrome whether changing the “change” variable to a lower value to make it loop less would help, but, instead figured out that for some reason it won’t loop with recursion at all. I had to set change to 0.00 for it to work at all.
Answer 4fb4cec7d56775000301a544
The issue was that the function
return howManyCoins(coinsSoFar);
isn’t defined; if you look, it’s
function howManyCoins (coinName, coinAmount, coinsSoFar)
This means that it no recursion takes place, and nothing will ever stop; this is the reason for
InternalError: Too much recursion.
Also, the error in the title is a stack overflow.
1 comments
But why does the lack of parameters result in a stack overflow instead of just an undefined statement?
Answer 4fb7a5c5a27f270003014966
If you’re having problems, check if you’re putting in the wrong number of arguments in your recursive call. That can cause some headaches.
I had the same problem until I looked at the hint (above) that they gave.
You’re passing in 3 parameters
function howManyCoins (coinName, coinAmount, coinsSoFar) {
but you are only passing out 1 of them in your recursive case. Try using all 3.
return howManyCoins(coinName, coinAmount, coinsSoFar + 1);
1 comments
But why does the lack of parameters result in a stack overflow instead of just an undefined statement?
Answer 4fb55e709bcf1e000305be43
Oh so I have to put in all the parameters when I use recursion?
Answer 4fb6b1cb5e7613000300f0d2
Answer 504faf972d7ee300020c36fb
Answer 4fb980150961810003041bb5
Thank you toby_s and bandicoots. I thought, because of the last exercise, you only needed to put in the parameter that is being modified.
Recursion has been working since then, so thank you again.
Answer 51504427321683dc3e00074b
Thanks for the code example Toby. Being a non-touch typist really hurts, I wasted so much time due to typo mistake. I mistyped ‘.’ instead of ‘,’ inside the parentheses, ‘chnge’ instead of ‘change’, capital letters…. etc
Popular free courses
- Free course
Learn SQL
In this SQL course, you'll learn how to manage large datasets and analyze real data using the standard data management language.Beginner Friendly4 Lessons - Free course
Learn JavaScript
Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.Beginner Friendly11 Lessons - Free course
Learn HTML
Start at the beginning by learning HTML basics — an important foundation for building and editing web pages.Beginner Friendly6 Lessons