CoDEVIANT #12 (4/12/19)

Guess who’s back, back again.
nuff said
No, not THAT Red Pill, fuck those people. 😡
That’s better.
You don’t feel personally attacked, WE feel personally attacked…by me….hmm…oh boy, I need therapy.
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
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;}
  • 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.
counter++ ; //shorthand way of doing counter = counter + 1;
You know HE’s happy.
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);   }}
  • 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);}
I love the smell of recursion in the morning.
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;}
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.
  • 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.

--

--

--

is a web developer, opera singer, actor, and lover of cats. (adrian-rosales.tech)

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Asynchronicity: An Intro to Asynchrony in JavaScript

How to upload source-map of your React project to Bugsnag

Vue 3 — Render Functions Basics

Developing Vue Apps with the Quasar Library — Timeline Options

ES6 — Start writing it today!

How to Make a Calendar App with Vue

Run Async Code on Update of a State with React Hooks

Forms in React: Controlled or Uncontrolled Components

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Adrian Rosales

Adrian Rosales

is a web developer, opera singer, actor, and lover of cats. (adrian-rosales.tech)

More from Medium

Beekeeper - Saving bees, supporting beekeepers

Dive Deep!

Development of Mobile App using Delphi

What is NodeJS?