On day 5 of the 100daysofcode challenge getting closer to working on Enum’s again. The structure of functions and init methods is one thing that made me start over on Treehouse. It bothers me when I can’t understand the process the compiler goes through to execute the code I write. There were to many instances where I was unclear about how something worked.

Nonetheless im working through the exercises for functions. This code challenge was all about writing if statements with the logical operators. The instructions were pretty simple.  n is a random integer.

If n is divisible by 3 print Fizz
If n is divisible by 5 print Buzz
If n is divisible by 3 & 5 print FizzBuzz
If n is neither print n

It’s a pretty simple couple of if statements to write out code that checks these requirements. I remember I had a problem with this one before and thought I wouldn’t this time but I got stuck. I wrote out all the conditions correctly but I was missing a fundamental concept. Scope is what I wasn’t taking into consideration.

Screen Shot 2017-07-21 at 8.34.16 AM

 

Even thought this code will compile correctly I can’t solve the problem with it. The problem is that if the random integer is divisible by 3 then we print Fizz and the code stops. The compiler doesn’t check the if statements that follow. So if its divisable by 3 and divisable by 3 & 5 the compiler doesnt ever get to checking if they’re both divisable. To fix this I have to take the scope into account and check if n is divisable by 3 & 5 first before any other if statements. Once I did this change I passed the challenge.

 

Screen Shot 2017-07-21 at 8.21.39 AM
Correct answer

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s