DEV Community

Cover image for 🌊 Baby Know Recursion!
JerryMcDonald
JerryMcDonald

Posted on • Edited on

🌊 Baby Know Recursion!

Note: This blog is not professional in any way; it's just for laughs! (mostly mine)

Alt Text

Alright! a little bitty "Paper, Rock, Scissors" recursion toy problem, and I am ready for it. Let's break it down. You have n number of rounds, so let's make a loop for n, then send the possible throw into recursion.

Alt Text

Dawg Gone it why is my recursive code not working? You use recursion on the next round, then try to do an array concatenate, use a spread operator here, how many loops should I put, wait...

No...
Oh no...
Can it be...
Alt Text
But I thought I was the recursion master!
Maybe my head is not 100% today; ok, forget recursion.
Leonidas didn't have "recursion" when he died fighting for Sparta!
I can do this with...
FOR LOOPS AND CONDITIONALS
LOTS AND LOTS OF FOR LOOPS AND CONDITIONALS
Let's get this code crackalackin!

Alt Text

When they see my code, they will say:
GOOD GOD, what determination! So many loops!

Alt Text

Holy Mathematicians of the past! It is just paper rock scissors!
Why is this taking so many loops!

Let me stop and think.

Ok, what advice did I hear one of my instructors tell me many years ago (2 weeks ago), if you hit a code wall: try to draw out the problem and visualize it.

And that is just what I did; I left my computer desk and hid in the corner, playing paper rock scissors with myself; my family looked on and thought I lost my mind.

If we want to use recursion and build an array the same length as the rounds given to us, maybe I should send the type of throw as the recursive variable.

Alt Text

But you would also want to continue with the original code run.

Alt Text

And in the forEach, you can use concat or a spread operator to account for the multiple arrays... GOT IT

Alt Text

I'm back on track! Call DeepMind and tell them I'll be able to start in August. Together, we will create an AI that will put us all in matrix chambers. No problem is too big and...

what is that in the distance approaching the city? Dear Keanu Reeves, it is massive! We are all doomed; it is the mother-ship of junior level recursion problems.
It is...
n-queens...
Alt Text
Alt Text
So we are going from paper rock scissors to chess? For real?

Well, this is starting to look like a bigger version of the paper rock scissors problem.

Alt Text

So we want to go down an entire length of column height. And send a recursive call at each index to check the next row for solutions while continuing on with our original code. That's the trick! Use recursion but don't stop with just one iteration!

Alt Text

Thanks for reading. I hope it made you smile!

Stay focused || love your code

Top comments (0)