10
Comments
  • 4
    HAHA nice.
  • 1
    ...yeah, of course, this second one is shorter and simpler, but
    1. i wrote the code as a translation of an idea from a language that allows items to reference other items of self, so i was locked into that for a moment

    2. where's the fun in it being more readable, even though it's shorter?
  • 1
    @Midnight-shcode yeah, I already understand the idea at first glance because when I started programming I saw some problems that involves fizzbuzz, even-odd, multiplesof35andboth. If someone is new to programming, I wouldn't recommend this one.
  • 2
    Func<int, string> fbz = (int n) => new string[] { n.ToString(), "Fizz", "Buzz", "FizzBuzz" }[Convert.ToInt32((n % 3) == 0) + Convert.ToInt32((n % 5) == 0) * 2];

    ok literally shortest i can make it, same idea, just stripping off extra cruft.

    i'm kinda proud of myself =D
  • 2
    [print([i,'Fizz','Buzz','FizzBuzz'][(i%3==0)+((i%5==0)<<1)])for i in range(1,101)][0]
  • 1
    @junon lisp? haskell?
  • 2
  • 0
    I have done a switch with the same idea in production. I don't remember what for, something about directions I think (north?+west?*2).
  • 0
    Oh, it's on!
  • 1
    99.times{|i|s=‘’;s+=‘Fizz’ if i%3==0;s+=‘Buzz’ if i%5==0;puts s.empty? ? i : s}

    I still think that could be shorter 🤔
  • 0
    @hamido-san @Root
    oh, it's on? then the same idea in JS which is probably the shortest i can make it in any language:

    fizzBuzz = (n)=>{
    return [n, "Fizz", "Buzz", "FizzBuzz"][((n % 3) == 0) + ((n % 5) == 0) * 2];
    }
  • 1
    @Midnight-shcode But that doesn’t loop!
  • 0
    @Midnight-shcode
    fb=n=>{for(i=1;i<=n;i++)console.log([i,f=‘Fizz’,b=‘Buzz’,f+b][!(i%3)+2*!(i%5)])};fb(99)

    Same approach, but a little shorter :)

    The argument list parenthesies are optional, so are the curlies on single-line blocks, and you can abuse js’s idea of truthiness to both simplify and do math on the comparisons. also the inline assignment is a bit shorter here.

    But with some js fanciness like ternaries plus further abuse of js’s truthiness and operation ordering, and moving the loop incrementor into the logic (saves one whole character!), you can shorten this a little more

    fb=n=>{for(i=0;i<n;)console.log((++i%5?’’:’Fizz’)+(i%5?’’:’Buzz’)||i)};fb(99)

    I think that’s as short as I’m able to go.
  • 0
    @Root
    the task specs say "write a function which when ran in a loop"

    the launching loop not a part of the task, that's an integration requirement for whomever uses the code, i included it in the first screenshot only to show the results to prove that it actually works :-P
  • 0
    @Root
    "and you can abuse js’s idea of truthiness to both simplify and do math on the comparisons"

    no i can't, that's why those casts are there, i was not allowed, how are you allowed?
    is it the difference between "n % 3 == 0" and "!(n % 3)" ? how would only a bool produced via "!" be truthiful, usable as arithmetic 1 ?
  • 1
    @Midnight-shcode Because JS. 😅

    bool+bool casts both bools as ints

    Also in my second example, ‘’+’’ returns ‘’, an empty string. And since its length is zero, when ||’d it gets casted to false
Add Comment