Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
kjing1934y@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.
-
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 -
[print([i,'Fizz','Buzz','FizzBuzz'][(i%3==0)+((i%5==0)<<1)])for i in range(1,101)][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).
-
Root826024y99.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 🤔 -
@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];
} -
Root826024y@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. -
@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 -
@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 ? -
Root826024y@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
Related Rants
-
BambuSource46I challenged my girlfriend (and myself) for the FizzBuzz thingy. I did it in js (because I suck at js) and she...
-
this6About 18 months ago my non-technical Manager of Applications Development asked me to do the technical intervie...
-
iSwimInTheC8Newcomer: I can do fizzbuzz in 6 lines of code Experienced: I can do fizzbuzz in 3 lines of code Professiona...
Lol.
Thoughts?
joke/meme
fizzbuzz