Skip to content

Instantly share code, notes, and snippets.

@renoirb
Last active December 20, 2015 17:19
Show Gist options
  • Save renoirb/6167713 to your computer and use it in GitHub Desktop.
Save renoirb/6167713 to your computer and use it in GitHub Desktop.
FizzBuzz or BarBaz implementations with multiples of 3 and 5. And misc. coding katas.
<?php
$i = 1;
do {
$str = NULL;
$m3 = is_int($i/3);
$m5 = is_int($i/5);
$str .= ($m3)?'Bar':NULL;
$str .= ($m5)?'Baz':NULL;
$str .= (!$m3 && !$m5)?$i:NULL;
echo $str.PHP_EOL; $i++;
} while($i < 2001);
'use strict';
var out = [],
current = [];
for (var i=1;i<101;i++) {
if (i % 3 !== 0 && i % 5 !== 0) {
out.push(i);
continue;
} else {
if (i % 3 === 0) {
current.push('Bar');
}
if (i % 5 === 0) {
current.push('Baz');
}
out.push(current.join(''));
current = [];
}
}

Rules

Iterate through numbers and when:

  • multiple of 3 to echo 'Bar' (or Fizz)
  • multiple of 5 to echo 'Baz' (or Buzz)
  • multiple of both 3 and 5 to echo 'BarBaz' (FizzBuzz)
  • otherwise, the number

Sample output

1
2
Bar
4
Baz
Bar
7
8
Bar
Baz
11
Bar
13
14
BarBaz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment