- 720p
- 540p
- 360p
- 0.50x
- 0.75x
- 1.00x
- 1.25x
- 1.50x
- 1.75x
- 2.00x
We hope you enjoyed this lesson.
Cool lesson, huh? Share it with your friends
About this lesson
Fizzbuzz! is a popular interview question/quiz. Let's build it!
Exercise files
Download this lesson’s related exercise files.
FizzBuzz!60.1 KB FizzBuzz! - Solution
59.5 KB
Quick reference
FizzBuzz!
Let's build FizzBuzz!
When to use
FizzBuzz is a traditional coder interview question, you might get asked it when applying for a job.
Instructions
The idea of FizzBuzz is to print out the number 1 to 100. If a number is divisible by 3, print out FIZZ; if divisible by 5, print out BUZZ; and if divisible by both 3 and 5, print out FIZZBUZZ!
There are many ways to do it, here's one way:
(1..100).each do |num|
if num % 3 == 0 && num % 5 == 0
puts "#{num}. FIZZBUZZ!"
elsif num % 3 == 0
puts "#{num}. Fizz!"
elsif num % 5 == 0
puts "#{num}. Buzz!"
else
puts "#{num}."
end
end
Hints & tips
- FizzBuzz is a popular interview question
- 00:04 In this video I want to talk about FizzBuzz.
- 00:06 And FizzBuzz used to be an old interview question that you would get when you
- 00:10 went into a coding job.
- 00:11 And it was a good question because you can do it in just about any
- 00:14 programming language, and it's a good way to sort of gauge what you know.
- 00:18 So we're going to step back now and take a little bit of a review of
- 00:21 all the stuff we've learned up until now and use it to create this FizzBuzz.
- 00:24 So the idea of FizzBuzz is pretty simple.
- 00:27 We want to print all the numbers to the screen between 1 and 100.
- 00:30 And if a number is divisible by 3, we want to print out the word Fizz.
- 00:35 If a number is divisible by 5, we want to print out the word Buzz.
- 00:39 And if a number is divisible by both 3 or 5, we want to print out FIZZ BUZZ.
- 00:44 So think about this, how would you do this yourself?
- 00:47 kind of try and think it through before we try it in this video.
- 00:49 You're going to obviously need a loop because we're going to have to
- 00:52 loop through between 1 and 100.
- 00:53 You're going to need to determine if a thing is divisible by a number.
- 00:57 That seems like sort of a job for a modulus.
- 00:59 And then we're going to have to do certain things based on that.
- 01:02 And that sounds like conditional statements, if and else if statements.
- 01:06 And we're testing against multiple things so
- 01:08 we're probably going to need an else if in there or maybe two.
- 01:11 So try and think it through and do it yourself.
- 01:13 But we're going to go ahead and right now just do it here.
- 01:15 So the first question to ask is what type of loop should you use?
- 01:18 And it absolutely doesn't matter one bit.
- 01:20 I probably would do a while loop because I like while loops,
- 01:23 but we were just talking about an each loop.
- 01:24 So let's start out with an each loop and end it.
- 01:28 And let's just puts out x.
- 01:31 So we have a range between 1 and 100.
- 01:33 And for each of those numbers, we want to do something with this variable x.
- 01:38 So let's save this and run it.
- 01:39 And I'm a big fan of making little incremental bits of code and
- 01:43 then testing them to see if they work.
- 01:44 So we can see this is printing out to 100, so that seems to work.
- 01:47 So now, let's start out by knocking out the big hard thing.
- 01:51 Determining if the number is divisible by both 3 and 5 at the same time.
- 01:55 And that seems like a job for a multiple conditional statement.
- 01:58 So let's go if x modulus 3 equals 0 and
- 02:02 x modulus 5 equals 0, then let's puts out x,
- 02:07 but we want to do some other stuff.
- 02:10 So let's do our interpolation.
- 02:13 And let's go x and let's put a period next to it just for
- 02:16 formatting to make it look nice.
- 02:17 And if it's divisible by both 3 and 5, we want to print out FIZZBUZZ.
- 02:21 So let's go FIZZBUZZ, right?
- 02:25 That looks good.
- 02:26 Now, this is the multiple conditional.
- 02:28 Let's break apart each of these two, the 3 and the 5,
- 02:32 and create a conditional statement for those.
- 02:35 And so of course we're going to use else if for that.
- 02:37 So else if x divisible by 3 equals 0.
- 02:43 And I'm just going to copy this line.
- 02:49 So if it's divisible by 3, we want to print out the number plus the word fizz.
- 02:52 I'm just going to delete that buzz there.
- 02:55 Let's put an exclamation mark just for fun.
- 02:58 So that handles the 3.
- 02:59 Now let's do the 5, same thing.
- 03:01 We'll just do another else if x modulus 5 equals 0.
- 03:06 And, again, I'm just going to paste this in and delete the fizz.
- 03:10 So if it's divisible by 5, we want to print out BUZZ.
- 03:13 Otherwise, we just want to print out the number.
- 03:15 So let's just go else and then do the same thing.
- 03:20 So that should just print out the number.
- 03:23 So finally we need to end our if statement.
- 03:25 So this end goes with this if.
- 03:27 This end goes with our loop.
- 03:29 And if we're lucky, go ahead and save this and run it and it looks pretty good.
- 03:34 So let's scroll back to the beginning.
- 03:37 So we have 1, 2, 3, 3 is divisible by 3, so it prints out FIZZ.
- 03:42 4, 5, 5 is divisible by 5, so we get BUZZ.
- 03:46 Up through here, and 15 is the first instance where it's divisible by both
- 03:50 3 and 5, so we get FIZZBUZZ.
- 03:52 And we just sort of eyeball this, scroll through here, 30 again,
- 03:55 we get another FIZZBUZZ, and it looks like it worked correctly.
- 03:59 All the way up to 100 which is divisible by 5 and we get BUZZ.
- 04:02 So that's FizzBuzz, very simple little program, very small,
- 04:06 just a few lines of code, but there's a lot of stuff packed in here.
- 04:09 Multiple conditional statements, modulus, else if, got our loop going on.
- 04:13 It's a good way to sort of practice some of the stuff we've done up until now.
- 04:16 So take a few minutes and try and redo this with a different type of loop.
- 04:20 And maybe try and do it with less lines of code, see if you can do that.
- 04:23 And it's just a really good way to kind of practice some of the stuff
- 04:26 we've learned up until now.
- 04:27 So that's FizzBuzz.
- 04:28 In the next video, we're going to look at hashes.
Lesson notes are only available for subscribers.