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
1
2
Bar
4
Baz
Bar
7
8
Bar
Baz
11
Bar
13
14
BarBaz
<?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 = []; | |
} | |
} | |