Locked lesson.
About this lesson
Exercise files
Download this lesson’s related exercise files.
Let's Build FizzBuzz!.docx58.8 KB Let's Build FizzBuzz! - Solution.docx
59.5 KB
Quick reference
Let's Build FizzBuzz!
FizzBuzz is a popular interview question for coding jobs.
When to use
FizzBuzz is a fun little app to put what we've learned so far to practical use.
Instructions
The idea of fizzbuzz is to print out all numbers between 1 and 100. If a number is divisible by 3, print fizz; if divisible by 5, print buzz, if divisible by 3 and 5, print fizzbuzz.
<?php
$x = 1;
while ($x <= 100) {
if ($x % 3 == 0 && $x % 5 == 0) {
echo "$x FIZZBUZZ!<br/>";
} elseif ($x % 3 == 0) {
echo "$x FIZZ!<br/>";
} elseif ($x % 5 == 0) {
echo "$x BUZZ!<br/>";
} else {
echo "$x<br/>";
}
$x++;
}
?>
Hints & tips
- FizzBuzz is a popular interview question
- 00:05 Okay, before we move on to more advanced topics, I wanna take a step back and
- 00:09 sort of review some of the things we've learned so far by building something,
- 00:13 a fun little app called FizzBuzz.
- 00:15 And Fizz Buzz is very popular for interviews,
- 00:18 you going to get a job as a coder, they give you this FizzBuzz problem and
- 00:22 they watch you create it to see how good of a coder you are.
- 00:25 It's just a standard interview question so I thought it would be fun for
- 00:28 us to do this.
- 00:29 So here is the essence of FizzBuzz.
- 00:31 Let's say you wanna print out numbers on the screen from 1 to 100.
- 00:35 If a number is divisible by three, print out the word Fizz.
- 00:38 If a number is divisible by five, print out Buzz.
- 00:41 If a number is divisible by three and five, print out FizzBuzz.
- 00:45 And just do that from 1 to 100.
- 00:47 So think about that for a second.
- 00:49 How would you do that if somebody asked you to do that?
- 00:52 Well, we actually have all the tools that we need.
- 00:54 You would use a loop because you wanna keep going from 1 to 100.
- 00:58 In the last couple of videos, we looped between 1 and 10, but you could easily
- 01:02 change that to 100, and we would use our comparison operators and our conditional
- 01:07 statements, our if statements, if a number is divisible by 3, do this, else do that.
- 01:12 Those are really all we need to do this.
- 01:14 So we want a number between 1 and 100.
- 01:16 Let's start out by creating a variable and set it equal to 0, or
- 01:20 if you wanna start with 1 or 2.
- 01:23 How about a while loop?
- 01:25 I like the while loops, while X is less than 100.
- 01:29 Or, you know what, lets go less than and equal to,
- 01:32 cuz we want to include 100, right?
- 01:35 Then do stuff.
- 01:37 So, what are we gonna do here?
- 01:39 Well, actually, just right here at the bottom, I'm gonna go ahead and
- 01:43 increment our variable so I don't forget.
- 01:46 So if, let's say x, now, how do we find out if a number is divisible by a number?
- 01:52 That's sounds like a modulus.
- 01:53 We learned that way back in the beginning of this course.
- 01:56 If x modulus 3 equals 0 meaning
- 02:01 if a number is divisible by 3, there's no remainder.
- 02:04 That remainder will be 0, and x modulus 5 equals 0.
- 02:12 Then echo, let's echo out our number and FIZZBUZZ,
- 02:18 oops, because that was one of the conditions.
- 02:23 We'll start out with the double first.
- 02:24 If a number is divisible by 3 and 5, print out FIZZBUZZ.
- 02:29 Else if, let's do our elseif.
- 02:33 Now, let's break these each down.
- 02:35 If x modulus 3 == 0, echo, and
- 02:40 I'm just gonna copy this and paste that.
- 02:47 FIZZ elseif x modulus 5 = 0,
- 02:52 echo, what do we say, BUZZ.
- 02:57 Else, if a number is not divisible by three, and
- 03:01 it's not divisible by five, and it's not divisible by three and
- 03:06 five, then we just want to echo out the number.
- 03:11 And let's go ahead and put line breaks
- 03:16 in each of these, so that it's not all on one line.
- 03:21 Now save this.
- 03:22 It looks pretty good.
- 03:23 Come back here and hit reload.
- 03:26 So we have 1, 2, 3: FIZZ!
- 03:28 4, 5: BUZZ!
- 03:30 6: FIZZ!
- 03:31 6 is divisible by 3.
- 03:33 7, 8, 9 is divisible by 3.
- 03:35 Down here, 15 is divisible by 3 and 5.
- 03:38 So it says FIZZBUZZ.
- 03:39 So we started at 1.
- 03:42 We went all the way to 100 and that's FizzBuzz.
- 03:46 I kid you not, a lot of jobs use this program or
- 03:50 this sort of thing as a test to see how good a programmer you are.
- 03:55 And you can see, this is very basic stuff, we just use our comparison operators,
- 03:59 our conditional statements in a quick and easy wild loop.
- 04:02 Think about how you would do this with a four loop and give that a try,
- 04:06 just for fun.
- 04:07 So that's FizzBuzz.
- 04:08 In the next video, we're going to start talking about arrays.
Lesson notes are only available for subscribers.