<?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: Brendon D'Souza</title>
    <description>The latest articles on DEV Community by Brendon D'Souza (@goa2usa).</description>
    <link>https://dev.to/goa2usa</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%2F817198%2F2085392e-0733-4200-b274-4f7aa23bf895.jpg</url>
      <title>DEV Community: Brendon D'Souza</title>
      <link>https://dev.to/goa2usa</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/goa2usa"/>
    <language>en</language>
    <item>
      <title>Day 0: Solving the FizzBuzz problem with JavaScript</title>
      <dc:creator>Brendon D'Souza</dc:creator>
      <pubDate>Thu, 17 Feb 2022 20:39:44 +0000</pubDate>
      <link>https://dev.to/goa2usa/day-0-solving-the-fizzbuzz-problem-with-javascript-10jj</link>
      <guid>https://dev.to/goa2usa/day-0-solving-the-fizzbuzz-problem-with-javascript-10jj</guid>
      <description>&lt;p&gt;I'm currently part of the #100devs program and our commander-in-chief, Leon Noel happened to mention the FizzBuzz challenge one time during office hours and I wanted to try it out. I thought, why not code it in javascript since thats the next language we will be diving into and I happen to know a little about loops and conditional statements from before.&lt;/p&gt;

&lt;p&gt;So, for those of y'all not familiar with the FizzBuzz coding challenge it goes something like this: &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The FizzBuzz problem is a classic test given in coding interviews. The task is simple:&lt;br&gt;
Print integers 1 to N, but print “Fizz” if an integer is divisible by 3, “Buzz” if an integer is divisible by 5, and “FizzBuzz” if an integer is divisible by both 3 and 5.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now that we all know what the challenge entails, let's get around to solving it.&lt;/p&gt;

&lt;p&gt;The first step is to decide which language you want to code it with. Like I previously mentioned, I'm doing it using JavaScript. You can do it with pretty much any programming language, the logic remains the same.&lt;/p&gt;

&lt;p&gt;Next, we set a definite scope for the problem. Currently the problem says to print integers from 1 to N, however to avoid using functions, I'm going to go from 1 to 100. Now that we have our scope, let's get to writing some code!&lt;/p&gt;

&lt;h2&gt;
  
  
  Structuring our code
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Variable declaration
&lt;/h3&gt;

&lt;p&gt;We start by defining our variable. I have used the variable name &lt;code&gt;i&lt;/code&gt; with the &lt;code&gt;let&lt;/code&gt; variable type so the value of &lt;code&gt;i&lt;/code&gt; can be modified down the line.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  What's the logic?
&lt;/h3&gt;

&lt;p&gt;Now we speak about the logic to solve such a problem. We know we need to print 100 lines, each consisting of either Fizz, Buzz, FizzBuzz or a number. In order to do the same task multiple times we need to use a &lt;em&gt;loop&lt;/em&gt;. I have used the &lt;code&gt;while&lt;/code&gt; loop.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;while&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;=&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A loop operates as follows:&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;while&lt;/code&gt; statement creates a loop that executes a block of statements inside it as long the test condition defined in the () remains true. The test condition is evaluated &lt;strong&gt;&lt;em&gt;before&lt;/em&gt;&lt;/strong&gt; the loop is entered into.&lt;/p&gt;

&lt;p&gt;In my &lt;code&gt;while&lt;/code&gt; statement, the loop will be executed as long as &lt;code&gt;i&lt;/code&gt; is less than or equal to 100. The reason I used &lt;code&gt;&amp;lt;=&lt;/code&gt; is because I have to run the code 100 times. Using only &lt;code&gt;&amp;lt;&lt;/code&gt; will cause it to run 99 times as we are starting from &lt;code&gt;i=1&lt;/code&gt; and not 0. &lt;/p&gt;

&lt;h3&gt;
  
  
  Visualize a flowchart
&lt;/h3&gt;

&lt;p&gt;We now need to define the statements inside the loop according to what we want to do.&lt;br&gt;
We basically have to do 1 of 4 different tasks for each line:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;If the number is divisible by 3 and 5, print "FizzBuzz".&lt;/li&gt;
&lt;li&gt;If the number is divisible by 3, print "Fizz".&lt;/li&gt;
&lt;li&gt;If the number is divisible by 5, print "Buzz".&lt;/li&gt;
&lt;li&gt;If the number is not a divisible by 3 or 5, then just print the number.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;In addition, we have to increment &lt;code&gt;i&lt;/code&gt; by 1 every time it gets printed.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; To check if a number is divisible by another number we use the &lt;code&gt;%&lt;/code&gt; operator. &lt;code&gt;%&lt;/code&gt; is the remainder operator and gives us the remainder when one number is divided by another. A number that is divisible by another has a remainder of 0, which is what we then test for using a comparison operator.&lt;/p&gt;
&lt;h3&gt;
  
  
  Convert your flowchart into conditional statements
&lt;/h3&gt;

&lt;p&gt;To make the computer understand the flowchart we spoke about above, we need to convert it into conditional statements. Conditional statements determine whether pieces of code can run or not.&lt;/p&gt;

&lt;p&gt;I will be using the &lt;code&gt;if&lt;/code&gt;, &lt;code&gt;else if&lt;/code&gt; and &lt;code&gt;else&lt;/code&gt; statements.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The &lt;code&gt;if&lt;/code&gt; statement&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To set the first condition from above, my syntax will be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="o"&gt;===&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="o"&gt;===&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;FizzBuzz&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If I had to convert the above line of code into simple English, I am basically telling the computer:&lt;/p&gt;

&lt;p&gt;If &lt;code&gt;i&lt;/code&gt; divided by 5 has a remainder of 0 &lt;strong&gt;and&lt;/strong&gt; &lt;code&gt;i&lt;/code&gt; divided by 3 has a remainder of 0, print "FizzBuzz" in the console. Once this statement is executed, increment the value of &lt;code&gt;i&lt;/code&gt; by 1.&lt;br&gt;
Remember, both conditions need to be true and hence the &amp;amp;&amp;amp; (logical and) operator is used.&lt;/p&gt;

&lt;p&gt;If the &lt;code&gt;if&lt;/code&gt; condition above returns true, then JavaScript skips any remaining conditionals and returns to the while loop condition.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The &lt;code&gt;else if&lt;/code&gt; statement&lt;/strong&gt;&lt;br&gt;
We then write &lt;code&gt;else if&lt;/code&gt; statements for the next two conditions. We are basically saying, if the first &lt;code&gt;if&lt;/code&gt; condition is not true, then test this condition to see if this condition is true.&lt;br&gt;
If true, then execute the statement block, &lt;code&gt;else&lt;/code&gt;, move on to the next &lt;code&gt;if&lt;/code&gt; statement and do the same.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;  &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="o"&gt;===&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Fizz&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="o"&gt;===&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Buzz&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;The &lt;code&gt;else&lt;/code&gt; statement&lt;/strong&gt;&lt;br&gt;
For the last condition, we are basically saying if you have reached thus far, that means the number isn't divisible by 3 or 5, so just print the number as is and then increment it by 1.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;!important:&lt;/strong&gt; We have to keep in mind to increment the value of &lt;code&gt;i&lt;/code&gt; every time it runs through the loop. If we do not increment &lt;code&gt;i&lt;/code&gt;, its value will always be 1 and the loop will run forever printing only the number 1.&lt;/p&gt;

&lt;h3&gt;
  
  
  Understanding how this all works
&lt;/h3&gt;

&lt;p&gt;When the code runs the first time, &lt;code&gt;i=1&lt;/code&gt; enters the &lt;code&gt;while&lt;/code&gt; loop since &lt;code&gt;i&lt;/code&gt; is less than 100 and gets tested by the first &lt;code&gt;if&lt;/code&gt; statement.&lt;br&gt;
Since &lt;code&gt;i=1&lt;/code&gt; is not divisible by 5 or 3, it is not allowed to enter and moves to the following &lt;code&gt;else if&lt;/code&gt; statement.&lt;br&gt;
Again, &lt;code&gt;i=1&lt;/code&gt; is not divisible by 3 so it is forced to move to the next &lt;code&gt;else if&lt;/code&gt; statement only to get rejected once again since it is not divisible by 5.&lt;br&gt;
Finally, &lt;code&gt;i=1&lt;/code&gt; gets to the &lt;code&gt;else&lt;/code&gt; statement which will run without any conditional testing and print the value of &lt;code&gt;i&lt;/code&gt; which is currently 1. The value of &lt;code&gt;i&lt;/code&gt; is then incremented by 1 courtesy of the &lt;code&gt;i++&lt;/code&gt; incremental operator. The reassigned value of &lt;code&gt;i&lt;/code&gt; is now 2.&lt;/p&gt;

&lt;p&gt;The whole process described above now repeats with &lt;code&gt;i=2&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This process continues to run a total of 100 times, each time the value of &lt;code&gt;i&lt;/code&gt; increasing by 1. After the while loop is executed 100 times, the value of &lt;code&gt;i&lt;/code&gt; is now 101. This time the &lt;code&gt;while&lt;/code&gt; loop will not execute as 101&amp;lt;=100 returns false. This causes the while loop to stop executing.&lt;/p&gt;

&lt;p&gt;And that is it! Your &lt;code&gt;while&lt;/code&gt; loop will run 100 times, all while printing what you have asked it to. The result looks something like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnrk00hog6cv0y39s0bz0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnrk00hog6cv0y39s0bz0.png" alt="console output"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;(PS: You can click this &lt;a href="https://codepen.io/goa2usa/pen/ZEavRQB?editors=0012" rel="noopener noreferrer"&gt;link&lt;/a&gt; for the entire solution from 1 to 100. I don't know how to embed a codepen here with the console showing, so if you do please teach me!)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I had fun coding this challenge and I rather enjoyed writing about it. This is the first time I've ever written something technical and it was rather fun to put down my learnings on paper! Thanks for reading! &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
