Skip to content

Instantly share code, notes, and snippets.

@resynth1943
Last active April 28, 2020 02:03
Show Gist options
  • Save resynth1943/6636747d1f350f304372f780ffe91b8c to your computer and use it in GitHub Desktop.
Save resynth1943/6636747d1f350f304372f780ffe91b8c to your computer and use it in GitHub Desktop.
Tiny FizzBuzz
for (
let /* index */ i = 0,
/* Buzz */ x,
/* Fizz */ y,
/* Buzz string */ l,
/* Empty string constant */ v = "",
/* Fizz string */ m,
/* Destructed the `console` for shorter code: */ { log: K } = console;
/* We need to do the FizzBuzz calculation 100 times. */ i <= 100;
/* And, of course, increment the index each time. Just a normal `for` loop so far. */ i++
)
/* This variable tells us whether or not this number is a `Buzz` number */ (x = !(i % 5)),
/* This variable contains whether or not this number is a `Fizz` number */ (y = !(i % 3)),
/* Then, we turn that into a string if it's a `Buzz` number (notice the ternary) */ (l = x ? "Buzz" : v /* Or, we just use our empty string constant defined above */),
/* Same as the above, but for `Fizz` */ (m = y ? "Fizz" : v),
/* Then we pass them to the logger. If both `Fizz` and `Buzz` strings are empty, it's obviously neither `Fizz` nor `Buzz`. In that case, print out the index instead. */ K(m + l || i);
for(let i=0,x,y,l,v='',m,{log:K}=console;i<=100;i++)x=!(i%5),y=!(i%3),l=x?'Buzz':v,m=y?'Fizz':v,K(m+l||i)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment