DEV Community

Amaboh
Amaboh

Posted on

Getting initiated into algorithmic thinking. How to think like a programmer for newbies like myself.

One of the most sort after skills as a developer is the ability to solve problems and most often in the world of computers this is synonymous to algorithmic thinking. In simple terms, this is the ability to break a task into smaller pieces and piece them together to get a task done.
SO why is this skill important as a young developer starting his or her journey you may be asking? Well as a software engineer you would always be task with writing softwares to solve human problems, and thus you need to know how to tell the computer how to perform a certain tasks. Since computers are design to perform as programmed, thus your job as a programmer is to tell the computer how to do perform a certain task and this involves a series of steps, which requires that you understand how to piece a program together to solve a problem.
SO explore this with this little initiation algorithm into Algo Thinking.

FizzBuzz
Write a program that uses console.log to print all the numbers from 1 to 100, with two exceptions. For numbers divisible by 3, print "Fizz" instead of the number, and for numbers divisible by 5 (and not 3), print "Buzz" instead.
When you have that working, modify your program to print "FizzBuzz" for numbers that are divisible by both 3 and 5 (and still print "Fizz" or "Buzz" for numbers divisible by only one of those).
(This is actually an interview question that has been claimed to weed out a significant percentage of programmer candidates. So if you solved it, your labor market value just went up.)

So how can we solve this some challenge???
Well as you think about it, take a moment to understand what the outcome of this program would be. If you've figured that already then congrats, you just completed one of the first important piece of the puzzle. Understanding the outcome, helps us piece together the process required to arrive at this imaginative result.

Ok let's put your assumption to the test, and see if your imaginative result is right. Before that I suppose u have an imaginative result like this.

1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz

If you had this in mind, then congrats on solving the first piece of the puzzle. Eventually, we can observe a pattern here, and with our young dev intuition, we can spot a loop. Is that right? Yes, I presume you just said that. If not then let's go back and think about it, if we're to make a program that goes through a series from 1 to 100, then obviously we would need to loop through and set conditions so that when each condition is met, then the program does goes into action mode to set our desired outcome. This brings us to the third part of our puzzle which is the using the if statement.

Thus we can summarize our algorithm into the following lines before writing our code.

/* loop through{
if (x == "value1") action1();
else if (x == "value2") action2(); else if (x == "value3") action3(); else defaultAction();
} */
;

function fizzBuzz(){
  for (let i = 1; i < 100 ; ++i){
    if( (i% 3 == 0) && (i % 5 ==0)){
      console.log("FizzBuzz")
    }else if(i % 3 == 0){
      console.log("Fizz")
    }else if( i % 5 == 0){
      console.log(" Buzz")
    }else{
      console.log(i)
    }
  }
}

fizzBuzz()
Enter fullscreen mode Exit fullscreen mode

Alright then, copy this and open the google Chrome on your laptop and press cmd + option + i, this would open the developer tools go to the console tab and paste this piece of code. Congrats on getting initiated but you just got started, so delete this and think about what is happen each time the loop is called until it arrives at the 100th digit. This is just to help you develop the thinking mindset of a programmer. I recommend you to use CodeWars if you haven't yet registered an account on the platform. Happy Hacking Dev.

Top comments (1)

Collapse
 
newveira profile image
Newman • Edited

Interessante! Isso mexe a sinapse. Gosto de ver essa capacidade de raciocínio em algoritmos.