Skip to content

Instantly share code, notes, and snippets.

@defenestrator
Last active May 17, 2022 04:45
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save defenestrator/f1cba3a097ba5dbacc3c025da1829b09 to your computer and use it in GitHub Desktop.
Some Fizzbuzz for meditation
// This one is interesting because no previously checked condition is re-checked
// It is a self-executing function that contains a declarative function named fizzbuzz() and a for loop
// Too much fun
(function() {
function fizzbuzz(i) {
const test = (d, s, x) => i % d == 0 ? _ => s + x('') : x
const fizz = x => test(3, 'Fizz', x)
const buzz = x => test(5, 'Buzz', x)
console.log(fizz(buzz(x=>x))(i.toString()))
}
for (let i = 1; i <= 100; i += 1){
fizzbuzz(i)
}
})()
for(i=0;i<100;)console.log((++i%3?'':'fizz')+(i%5?'':'buzz')||i)
// Exceedingly fast approach
// Array is concatenated to a single newline separated string and output
Array.apply(0, Array(100)).map(function (_, i) {
i += 1
const fizzes = i % 3 == 0
const buzzes = i % 5 == 0
const fizzBuzz = (fizzes && buzzes)
if (fizzBuzz) return 'FizzBuzz'
if (fizzes) return 'Fizz'
if (buzzes) return 'Buzz'
return i
}).join("\n")
// This is a decent answer to the deceptively awesome interview question of fizzbuzz
for(i=0; i<=100;i++){
let x=''
if (!(i%3)) x +='Fizz'
if (!(i%5)) x +='Buzz'
console.log( x || i.toString() )
}
<?php
// no 'if' nor '%' operator, so there's that.
$step[0] = 3;
$step[1] = 5;
$step[2] = 15;
for ($i = 1; $i<=100; $i++) {
$x[$i] = $i;
}
for ($i = $step[0]; $i <= 100; $i = $i + $step[0]) {
$x[$i] = "Fizz";
}
for ($i = $step[1]; $i <= 100; $i = $i + $step[1]) {
$x[$i] = "Buzz";
}
for ($i = $step[2]; $i <= 100; $i = $i + $step[2]) {
$x[$i] = "FizzBuzz";
}
echo(implode(PHP_EOL, array_values($x)));
<?php
// There are only 10 kinds of people in the world
// People who understand binary and people who don't
// And people who understand ternary
for($i=0;$i++<100;)echo($i%3?'':'Fizz').($i%5?'':'Buzz')?:$i,"\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment