Profile image of anonymous
Submitted by anonymous
over 12 years

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

15 votes

Permalink

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.

Profile image of anonymous
Submitted by anonymous
over 12 years

1 comments

Profile image of d14a248
Submitted by d14a248
over 12 years

But why does the lack of parameters result in a stack overflow instead of just an undefined statement?

Answer 4fb7a5c5a27f270003014966

8 votes

Permalink

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);
Profile image of bandicoots
Submitted by bandicoots
over 12 years

1 comments

Profile image of d14a248
Submitted by d14a248
over 12 years

But why does the lack of parameters result in a stack overflow instead of just an undefined statement?

Answer 4fb55e709bcf1e000305be43

1 vote

Permalink

Oh so I have to put in all the parameters when I use recursion?

Profile image of anonymous
Submitted by anonymous
over 12 years

Answer 4fb6b1cb5e7613000300f0d2

1 vote

Permalink

line 7 return howManyCoins(coinName, coinAmount, coinsSoFar + 1);

I feel like the author is like “Hey here is recursion….good luck!”

And I am like Huh?

Profile image of x13420x
Submitted by x13420x
over 12 years

Answer 504faf972d7ee300020c36fb

1 vote

Permalink

The fact that you must include all of the parameters should definitely be one of the hints.

Profile image of reddog
Submitted by reddog
over 12 years

Answer 4fb980150961810003041bb5

0 votes

Permalink

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.

Profile image of anonymous
Submitted by anonymous
over 12 years

Answer 51504427321683dc3e00074b

0 votes

Permalink

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

Profile image of netRunner82981
Submitted by netRunner82981
almost 12 years