<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: oksanadev</title>
    <description>The latest articles on DEV Community by oksanadev (@oksanadev).</description>
    <link>https://dev.to/oksanadev</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F439160%2F91658cbb-8a77-4dc6-bd0d-91b8e0a626e6.png</url>
      <title>DEV Community: oksanadev</title>
      <link>https://dev.to/oksanadev</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/oksanadev"/>
    <language>en</language>
    <item>
      <title>Recursive FizzBuzz in JavaScript </title>
      <dc:creator>oksanadev</dc:creator>
      <pubDate>Mon, 09 Nov 2020 14:44:31 +0000</pubDate>
      <link>https://dev.to/oksanadev/recursive-fizzbuzz-in-javascript-46bi</link>
      <guid>https://dev.to/oksanadev/recursive-fizzbuzz-in-javascript-46bi</guid>
      <description>&lt;p&gt;The Fizz Buzz test is a simple example of a tech interview question designed to test job candidates. It reads as follows:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Write a function that prints the numbers from 1 to 100. For multiples of three print “Fizz” instead of the number and for multiples of five print “Buzz” instead of the number. For numbers which are multiples of both three and five print “FizzBuzz”.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It is a basic exercise to practice conditionals and modulo/remainder operator. If you can think of one obvious solution, you might be surprised to see that there are lots of ways this can be solved in JavaScript. For example, &lt;a href="https://ditam.github.io/posts/fizzbuzz/"&gt;this article &lt;/a&gt; suggests 20 JavaScript ways to solve FizzBuzz!&lt;br&gt;
The classic JavaScript solution though would look like this (using an old-fashioned for loop):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for (let i = 1; i &amp;lt;= 100; i++) {
   if (i % 3 === 0 &amp;amp;&amp;amp; i % 5 === 0) console.log('FizzBuzz');
   else if (i % 5 === 0) console.log('Buzz');
   else if (i % 3 === 0) console.log('Fizz');
   else console.log(i);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you love one-liners, there is also a short version of it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for (let i = 1; i &amp;lt;= 100; i++) console.log(i % 3 ? i % 5 ? i : 'Buzz' : i % 5 ? 'Fizz' : 'FizzBuzz')
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;However, as our code is supposed to be read by humans (the machine will read it in any form), developers are generally advised against making the life of fellow programmers even more complicated than it already is.&lt;/p&gt;

&lt;p&gt;While moving forward in my coding journey, I have attempted to implement a recursive version of FizzBuzz.&lt;/p&gt;

&lt;p&gt;Recursion is the act of a function calling itself. Thus, recursion occurs any time a function calls itself inside itself, potentially creating an infinite loop &lt;a href="https://developer.mozilla.org/en-US/docs/Glossary/Recursion/"&gt;Source&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;A recursive function has 2 main ingredients: a base or exit condition and at least one recursive case. No exit condition would lead to an infinite loop.&lt;/p&gt;

&lt;p&gt;A basic solution for a recursive FizzBuzz would be to create an inner recursive function that uses the initial value of the counter variable declared in the scope of the parent function. It represents a closure in a broader context:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;You have a closure when a function accesses variables defined outside of it.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://whatthefuck.is/closure/"&gt;www.whatthefuck.is/closure/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In our case,  the basic condition states: “if the parameter passed to the inner function (counter) is greater than the parameter passed to the main function - 100, exit the function”.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const fizzBuzz = (randomNum) =&amp;gt; {
   let counter = 1;
   const func = (counter) =&amp;gt; {
      if (counter &amp;gt; randomNum) return;

      if (counter % 5 === 0 &amp;amp;&amp;amp; counter  % 3 === 0) console.log('FizzBuzz');
      else if (counter % 5 === 0) console.log('Buzz');
      else if (counter  % 3 === 0) console.log('Fizz');
      else console.log(counter);

      func(counter + 1);
   };
   func(counter);
}
fizzBuzz(100)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;However, this can be simplified by using default values of parameters (ES6 feature). So, the cleaner version would look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const fizzBuzz = (randomNum, counter = 1) =&amp;gt; {
   if (counter &amp;gt; randomNum) return;

   if (counter % 5 === 0 &amp;amp;&amp;amp; counter  % 3 === 0) console.log('FizzBuzz');
   else if (counter % 5 === 0) console.log('Buzz');
   else if (counter  % 3 === 0) console.log('Fizz');
   else console.log(counter);

   fizzBuzz(randomNum, counter + 1);
};

fizzBuzz(100)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The counter parameter is assigned a default value of 1. Thus, we don’t need the inner function anymore as its only role was to grab the initial counter value from the outer scope.&lt;/p&gt;

&lt;p&gt;As a bonus, here is a recursive FizzBuzz running downwards:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const fizzBuzz = (randomNum) =&amp;gt; {
   if (randomNum === 0) return;

   if (randomNum % 5 === 0 &amp;amp;&amp;amp; randomNum % 3 === 0) console.log('FizzBuzz');
   else if (randomNum % 5 === 0) console.log('Buzz');
   else if (randomNum % 3 === 0) console.log('Fizz');
   else console.log(randomNum);
   fizzBuzz(randomNum - 1);
}

fizzBuzz(100)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Recursion might be difficult to grasp at first. These simple examples of the classic programming puzzle were designed with an aim to make this topic a little bit clearer.&lt;/p&gt;

&lt;p&gt;Cover photo by &lt;a href="https://unsplash.com/@celinehaeberly"&gt;Céline Haeberly&lt;/a&gt; on Unsplash &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>recursion</category>
      <category>closure</category>
    </item>
  </channel>
</rss>
