# CoDEVIANT #12 (4/12/19)

I’ve been away for a minute, traveling and such. The rumors of my dwindling enthusiasm for solving arbitrary problems written by a gaggle of pasty assholes…is quite accurate. By all rights, I should just load up Super Smash Brothers Ultimate and try to get into Elite Smash only to get spiked by a 12 year old on her Twitch stream…but that’s happened so many times before.

So instead I’m going to take the red pill

But the metaphorical red pill of my own context which is to buckle down and try to make my mom proud.

*Fun fact, I once rode a sled made from a box for a giant keyboard down a flight of apartment stairs. I also walked out of my car while it was still moving on accident, the car proceeded to crash into the gate of a storage unit facility. And I also ran through a screen-door when I lost my temper. The LAST one was from when I was a kid. Case in point: I think remembering to not suffocate in my sleep must make her head swell with an odd combination of pride and pity. Wait til she sees this article!

Problem 1: Persistent Bugger (What a name ❤️…it’s just wearing me down 😅)

Write a function, persistence, that takes in a positive parameter num and returns its multiplicative persistence, which is the number of times you must multiply the digits in num until you reach a single digit.

For example:

How Adrian tried to solve it while drunk last night…

…let’s not embarrass myself TOO TOO much. Let’s think of my poor mother.

How Adrian Solved it in the morning…(after almost quitting, so go him!…I mean me)

It’s a little wild and my comments are still in there because

• we need to create a counter that starts at zero, because this is the value we will eventually return
• Then if we look down to the blue area you’ll see what happens next, despite all visual cues, to normal people, to the contrary.
• I say that if we turn the number are given into a string
• Then if we check the length of that string
• And see that the length equals 1
• Then we will return counter to a wider scope of our function allowing us to spit out a value ultimately. *The purple text*
• In such cases, counter would equal zero and we would be happy.

HOWEVER

If the length is NOT 1, then we have work to do, we increase counter by one

and then we call our function that we created inside this function

this inner function, I named recursiveWork and it takes the num value as an argument.

Behold recursiveWork

In recursiveWork:

• We make num equal itself turned into a string and made into an array
• We realize a need to multiply each member of the array from left to right, and thus a variable [runningTally set at 1 to start] to hold the value of the result of us using the .forEach method on our newly minted array num
• We set up .forEach( ) on num and pass it the current value being analyzed during a loop that is implied in the method which we name, quite creatively so, currentValue. Yeah, I know…it’s boring.
• Then inside the code block of the .forEach( ) method’s use, we make runningTally equal itself times currentValue.
• After this, we enter into an if-statement where we say “If runningTally, made into a string, has the length of one, then we will return counter as it is.
• This escapes us from the recursive nature of the function recursiveWork (which we’ll get to further in a second) and back into the wider scope of the original function PAST the if-statement that got us into having to use recursiveWork to begin with and returning counter.
• If, however, runningTally, made into a string, does NOT have the length of one, then we increment counter by one and call recursiveWork( ) again *from WITHIN recursiveWork crazy I know!* passing in runningTally as its argument

And then it goes on over and over again until Neo picks the correct doo — I mean, until runningTally made into a string has the length of one.

Final Solution:

How smarter people did it

• They make a variable to hold the number of times they had to multiply the contents of num made into an array by itself. times
• They make num equal itself as a string
• While num has a length that is bigger than 1
• They increment times by 1
• They make num into an array with .split(‘’)
• They create a new array from it using .map( ) passing in the word Number which in the context of the .map( ) method parses the elements into integers.
• With an array of integers they use reduce( ) and pass in a to be the accumulated value this far in the reduction process [basically outputting the array’s content as a single value] and b to stand in for the current value in the array.
• In this way we create the new array as something from multiplying the previous accumulated value, which will begin by being the value of array[0] by default, multiplied by the current value (b).
• Then they turn it into a string with .toString( ) and because we are inside of a while-loop that evaluates the length of num, if after all that, num has a length that is greater than one, we go on and do this again.
• Once num has a length that is NOT bigger than one:
• We return times.

What I learned

• That you can use Number with .map( ) and have it parse integers for values in an array you’re creating…that’s really dope. I tried placing “Cat” in there just to see if I could break it…and break it I did.
• That while loops can be awesome if you know how to wield them without breaking your system. They tend to do that more often, so I tend to stick to if-statements, switches, and for-loops
• To never give up…because I almost did, especially when I tried this after some brandy last night. It was not pretty.

So I know I should do at least another but I have other projects to work on that will actually get me paid. I love government cheese as much as the next person, but variety is the spice of life…and lactose intolerance runs in the family.

Later, nerds.

--

--