CoDEVIANT #12 (4/12/19)

Adrian Rosales
8 min readApr 12, 2019

--

Guess who’s back, back again.

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.

nuff said

So instead I’m going to take the red pill

No, not THAT Red Pill, fuck those people. 😡

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

That’s better.

*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!

You don’t feel personally attacked, WE feel personally attacked…by me….hmm…oh boy, I need therapy.

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:

persistence(39) === 3 // because 3*9 = 27, 2*7 = 14, 1*4=4// and 4 has only one digitpersistence(999) === 4 // because 9*9*9 = 729, 7*2*9 = 126,// 1*2*6 = 12, and finally 1*2 = 2persistence(4) === 0 // because 4 is already a one-digit number

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)

function persistence(num) {   console.log(num);//we need to create a counter that starts at zero   let counter = 0;function recursiveWork(num){//we need to turn num into a string and the into an array   num = num.toString().split(‘’);   console.log(num);//we need to multiply each member of the array from left to right by each other//we will need a variable to hold the value of the forEach’s work   let runningTally = 1;   num.forEach((currentValue)=>{      runningTally *= currentValue;      console.log(runningTally);   })console.log(‘runningTally is: ‘ + runningTally);console.log(‘counter is currently:’ + counter);//IF the final product is one character long, we return counter which is zero   if(runningTally.toString().length == 1) {      return counter;   } else {//ELSE (should the final product be longer than one character) we increment the counter//by one and then turn the final product into a string and into an array (repeat)      counter++;      recursiveWork(runningTally);   }}   if(num.toString().length == 1){      return counter;   } else {      counter ++;   recursiveWork(num);   }return counter;}

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

My thought process was this:

  • we need to create a counter that starts at zero, because this is the value we will eventually return
let counter = 0;
  • 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
num.toString()
  • Then if we check the length of that string
num.toString().length
  • And see that the length equals 1
if( num.toString().length == 1 ) {//we all float down here.... shut up, Pennywise.}
  • 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

counter++ ; //shorthand way of doing counter = counter + 1;

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

You know HE’s happy.

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

Behold recursiveWork

Shit, wrong ‘work’ but also really really really good.
function recursiveWork(num){//we need to turn num into a string and the into an array   num = num.toString().split(‘’);   console.log(num);//we need to multiply each member of the array from left to right by each other//we will need a variable to hold the value of the forEach’s work   let runningTally = 1;   num.forEach((currentValue)=>{      runningTally *= currentValue;      console.log(runningTally);   })   console.log(‘runningTally is: ‘ + runningTally);   console.log(‘counter is currently:’ + counter);//IF the final product is one character long, we return counter which is zero   if(runningTally.toString().length == 1) {      return counter;   } else {//ELSE (should the final product be longer than one character) we increment the counter//by one and then turn the final product into a string and into an array (repeat)      counter++;      recursiveWork(runningTally);   }}

In recursiveWork:

  • We make num equal itself turned into a string and made into an array
num = num.toString().split(‘’); // 36 becomes ’36’ before turning into [‘3’,’6’]
  • 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.
My favorite season is spring, I collect stamps, my favorite sexual position is missionary, and my favorite super hero is Superman
  • Then inside the code block of the .forEach( ) method’s use, we make runningTally equal itself times currentValue.
runningTally *= currentValue;// shorthand for runningTally = runningTally * 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.
and that’d be it.
  • 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
if(runningTally.toString().length == 1) {   return counter;   } else {   counter++;   recursiveWork(runningTally);}

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.

I love the smell of recursion in the morning.

Final Solution:

function persistence(num) {   let counter = 0;   function recursiveWork(num){      num = num.toString().split(‘’);      let runningTally = 1;      num.forEach((currentValue)=>{         runningTally *= currentValue;      })      if(runningTally.toString().length == 1) {         return counter;      } else {         counter++;         recursiveWork(runningTally);      }}   if(num.toString().length == 1){      return counter;   } else {      counter ++;      recursiveWork(num);   }return counter;}

How smarter people did it

function persistence(num) {   var times = 0;   num = num.toString();   while (num.length > 1) {   times++; num = num.split(‘’).map(Number).reduce((a, b) => a * b).toString();}return times;}
  • 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.

--

--