FizzBuzzing Part 1

Today in class I created again one of the first programs I ever attempted to build: FizzBuzz. My first FizzBuzz was with JavaScript about two months ago. To be honest, I liked the practice that got making FizzBuzz so much that I even attempted it with Java, C, and ObjectiveC. Yeah, I am a curious guy.

When Ashley, one of our amazing instructors, asked students to try FizzBuzz, I had a ear to ear grin o my face. However, my smiley face became suddenly quizzical as soon as she mentioned doing it in Ruby.  It caught me unprepared because I was not 100% confident in using loops in this language. At least, I wasn’t sure which one to use as there are a bazzilion ways to code them, methods such as “.times”, “.each” are only the tip of the iceberg.

Luckily, Ashley requested that students also create FizzBuzz in Javascript for bonus points. Little did she know that through that she actually facilitated immensely my work.

I delved right into JavaScript. This is my code:

For me the loop syntax in the JavaScript code above feels very natural . I can’t tell you  if it is because I have been practicing with it for a while that I created a habit of using it or if it is because of its clear distinction among the state, condition and increment of the loop.

The state is the i = 1; part; it declares a variable to use for the counting, and sets its start value–here it has the start value of 1. The variable exists only within the loop (down through the ”  } ” of the body.

Typically, the test is a boolean expression such as ” i < 101″, showing what should be true for the loop to continue.

Finally, the increment code executes and then the program loops back to the state. The increment advances the state of things in preparation for the next iteration. The “i++” means incrementing the variable “i” by one. It is the same thing as i = i +1.

So we have when it should start i = 1, when it should stop, when i < 101, and by how much it is adding in each iteration i+1. In my opinion this  is explicit and clear.

In Ruby, I learned today, that the state, condition and increment is hidden in the object method. In the end I managed to create my FizzBuzz in Ruby. Check out the code below.

Crazy, huh? Where are the loop condition and  state ? The only bit noticeable is the increment in “101.times” . Ashley actually told us that all (condition, state and increment) are in the .time method. In other words, it is “magic” as she put it. I agree with the argument that reading “101.times” is easier than “for(var i=0; i <101; i++)”, but what students gain in convenience they lack  in loop mechanics comprehension. I know well about loop because I played with it and  repeatedly tried and erred with the inherent aspects of this function while I learning JavaScript. I don’t think I would understand loop as fully as I understand know if my only exposure to it was through Ruby. I can say that it was because I learned in JavaScript first that I was able to breeze through the Ruby version of FizzBuzz I created.

Call me loopy, but I would advise a beginner to learn loops with JavaScript first and then later learn it the Ruby way. In my opinion, the initiated is more likely to get this important programming concept in this fashion.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s