Roll20 uses cookies to improve your experience on our site. Cookies enable you to enjoy certain features, social sharing functionality, and tailor message and display ads to your interests on our site and others. They also help us understand how our site is being used. By continuing to use our site, you consent to our use of cookies. Update your cookie preferences .
×
Create a free account

Recursion not working

I'm trying to program a particular type of exploding dice. Roll a d10. If it's 10, roll and add another d10, etc. However, if the first  (and only the first) roll is a 1, roll again and subtract from 0. This negative die also explodes on 10, making the number more negative. Here's the code. The problem I seem to be having is that when a 1 or 10 is rolled, the function is supposed to call itself recursively, but that's not happening. I've left my troubleshooting log() calls in. function runeRoll(iter) {     var d10 = randomInteger(10);     log(d10);     log(d10 > 1 && d10 < 10);     log("iter: " + iter);     if(d10 > 1 && d10 < 10) return d10;     if(d10 === 1 && iter > 0); return d10;     if(d10 === 1 && iter === 0); return -1 * runeRoll(++iter);     if(d10 === 10) return d10 += runeRoll(++iter); } I call the function with runeRoll(0).
1473371729

Edited 1473372006
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
I'd suggest changing your if chain to this: function runeRoll(iter) {     var d10 = randomInteger(10);     log(d10);     log(d10 > 1 && d10 < 10);     log("iter: " + iter);     if(d10 === 1 && iter > 0){return d10; }else if(d10 === 1 && iter === 0){return -1 * runeRoll(++iter);     }else if(d10 === 10){return d10 += runeRoll(++iter); }else{return d10;}; } No real benefit to it, just preference. Have you tried putting log calls inside the if statements to see if they are being entered? I'd also look at the differences in how the if statements end, you have some that are separated from their respective return statements by a semicolon, and some that aren't. You could also look at using sendChat() and Promises to use the quantumRoll parser to resolve the d10. This won't have any real difference in the result since the API uses quantum roll for random number generation, but who knows if it might not resolve some unknown issue. EDIT: Also, try putting your more complicated return statements inside parentheses: return (-1 * runeRoll(++iter));
Scott C. said: I'd also look at the differences in how the if statements end, you have some that are separated from their respective return statements by a semicolon, and some that aren't. OH MY--- WHO PUT THOSE SEMICOLONS THERE?! It certainly wasn't m--LOOK! SQUIRREL!!!
1473374546
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
Hahaha, I take it it works now? Welcome to the "What the %$&& did I do that for?" club. I don't know how many times I've done something like that.
Yeah, it works now. Thanks.
1473378166
Andrew C
Marketplace Creator
*points out the window* look it's a decoy!
1473382863
The Aaron
Pro
API Scripter
I think the number of times I've written stupid code on the first try is probably roughly equivalent to the number of times I've written code.  =D
1473396597
Lithl
Pro
Sheet Author
API Scripter
Andrew said: *points out the window* look it's a decoy! LOOK! A great stampeding herd of well-placed distractions!