Last active
December 3, 2020 23:05
-
-
Save webstrand/888e1b26c2db3303c1133b5606e4fdfa to your computer and use it in GitHub Desktop.
Code-golfed 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
| // [...Array(101)] : is the shortest way to generate a range from [0..100] | |
| // .map((_,n)=> : The array is filled with undefined, so we use the index instead. | |
| // "Fizz".slice(n%3&&4): (n%3) is truth-y for all non-multiples of 3. (n%3)&&4 is either 0 or 4. | |
| // ||n : "Fizz".slice(4)+"Buzz".slice(4) is "", which is false-y, so we replace it with n | |
| // .find : .map returns an array, .find does not. | |
| [...Array(101)].map((_,n)=>"Fizz".slice(n%3&&4)+"Buzz".slice(n%5&&4)||n).find(s=>console.log(s)) | |
| // An interesting, but longer variation: | |
| // n%3&&1-n%5&&4 : returns either 4 for Buzz or 0 for Fizz and FizzBuzz. (other values are excluded by the length arg) | |
| // (!(n%3)+!(n%5))*4 : returns 0, 4, or 8 for none, Fizz or Buzz, or FizzBuzz respectively. | |
| [...Array(100)].map((_,n)=>"FizzBuzz".substr(n%3&&1-n%5&&4,(!(n%3)+!(n%5))*4)||n).find(s=>console.log(s)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment