Created
January 30, 2014 11:52
-
-
Save ruckuus/8706980 to your computer and use it in GitHub Desktop.
FizzBuzz
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| class Numlet{ | |
| protected $fizz = false; | |
| protected $buzz = false; | |
| public function isFizz($n) { | |
| if ($n % 3 == 0) { | |
| $this->fizz = true; | |
| return true; | |
| } | |
| } | |
| public function isBuzz($n) { | |
| if ($n % 5 == 0) { | |
| $this->buzz = true; | |
| return true; | |
| } | |
| } | |
| public function isNormal($n) { | |
| if (!$this->fizz && !$this->buzz) { | |
| return false; | |
| } | |
| } | |
| public function fizzBuzz($n) { | |
| if ($this->isFizz($n)) { | |
| echo "Fizz "; | |
| } | |
| if ($this->isBuzz($n)) { | |
| echo "Buzz "; | |
| } | |
| if ((!$this->fizz) && (!$this->buzz)) { | |
| echo $n . " "; | |
| } | |
| } | |
| } | |
| for ($i = 1; $i < 20; $i++) { | |
| $x = new Numlet(); | |
| $x->fizzBuzz($i); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| $out = array_map(function($n) { return (''==($m=(($n % 3 == 0) ? "Fizz" : '') . ( ($n % 5 == 0) ? "Buzz" : '')) )?$n:$m; }, range(1,100)); | |
| foreach($out as $i) { | |
| echo "${i}\n"; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment