Fizz Buzz in CSS

(susam.net)

28 points by froober2 hours ago

3 comments

  • susam54 minutes ago
    A shorter solution is possible with an ordered list (&lt;ol&gt;) if we&#x27;re willing to ignore the untidy output:<p><pre><code> li:nth-child(3n), li:nth-child(5n) { list-style: none } li:nth-child(3n)::before { content: &quot;Fizz&quot; } li:nth-child(5n)::after { content: &quot;Buzz&quot; } </code></pre> Example: <a href="https:&#x2F;&#x2F;susam.net&#x2F;code&#x2F;web&#x2F;css-fizz-buzz-ol.html" rel="nofollow">https:&#x2F;&#x2F;susam.net&#x2F;code&#x2F;web&#x2F;css-fizz-buzz-ol.html</a><p><pre><code> $ curl -sS https:&#x2F;&#x2F;susam.net&#x2F;code&#x2F;web&#x2F;css-fizz-buzz-ol.html | sed -n &#x27;&#x2F;none&#x2F;,&#x2F;after&#x2F;p&#x27; | tr -d &#x27;[:space:]&#x27; li:nth-child(3n),li:nth-child(5n){list-style:none}li:nth-child(3n)::before{content:&quot;Fizz&quot;}li:nth-child(5n)::after{content:&quot;Buzz&quot;} $ curl -sS https:&#x2F;&#x2F;susam.net&#x2F;code&#x2F;web&#x2F;css-fizz-buzz-ol.html | sed -n &#x27;&#x2F;none&#x2F;,&#x2F;after&#x2F;p&#x27; | tr -d &#x27;[:space:]&#x27; | wc -c 129 </code></pre> But I don&#x27;t quite like how misaligned the numbers and the words look in this version. Correcting that would call for extra code that would cancel out the bytes saved.
    • cluckindan31 minutes ago
      <p><pre><code> list-style-position: inside;</code></pre>
      • susam21 minutes ago
        Yes! However, like I mentioned in my previous comment, corrections like this cancel out the number of bytes saved with the &lt;ol&gt;-based solution.<p>I mean, the solution in the original post is 152 characters long.<p>The &lt;ol&gt; based solution is 129 characters long. Shorter but uglier.<p>If we add your correction, we get neater output, which is nice, but it comes at the cost of 30 additional characters in the minified code thereby making the solution 159 characters long.<p><pre><code> li { list-style-position: inside } li:nth-child(3n), li:nth-child(5n) { list-style: none } li:nth-child(3n)::before { content: &quot;Fizz&quot; } li:nth-child(5n)::after { content: &quot;Buzz&quot; }</code></pre>
  • kevinsync57 minutes ago
    I love this, it&#x27;s a very clever and funny way to solve the problem. Makes me think about how there are infinite routes from A to B, some more scenic and whimsical than others.. as well as all the people I&#x27;ve met along the way who would be so pissed and pedantic about how this isn&#x27;t a &quot;real solution&quot; LOL
    • hyperhello5 minutes ago
      The problem is that you have to define the problem enough to avoid the fact that it&#x27;s trivial to output the string &quot;1,2,Fizz,4,Buzz,6,......&quot; and fulfill the assignment. You can, in fact, output &quot;$1,$2,Fizz,$4,Buzz,$6...&quot; where $ is any prefix itself divisible by 15 (there are other templates for the other situations) but it clearly does repeat endlessly.
  • carl_dr50 minutes ago
    Ignoring the size of the HTML in addition to the CSS, it’s fun, but not really fair when talking about code golf. Beyond a few numbers, you need to include some JavaScript and generating a million list elements. But those bytes count …