Inside of it there is a single console.log call, that has one argument. All of the magic is in this argument.
This argument consists of two parts:
[array][index access]
Left part is the initialization of an array with 4 things: i, fizz, buzz and fizzbuzz.
Right part is more complicated.
i % 3 resolves to "true" whenever i is divisible by 3. But, when boolean value is used in a mathematical operation (and << is one of them), it resolves to 0 when false and 1 when true.
x << y is a bit shift operator, which moves all digits of bit representation of x left (and >> right) y times, for example: 0010 << 2 = 1000 = 8
so, for i = 10 a part of the expression goes like this:
this value is then ORred using | with 0 (received from similar construction for i % 3), which gives 2 back
this accesses 3rd element of the array which is buzz, and console logs it.
Sorry if formatting is bad, phone.
edit: btw, i%3 bitshifted by 0 which doesn't change the number but because this is a math operation, it transforms it into a number in order to access the array with a number.
i % 3 is not a boolean operation, and it does not resolve to true when i is divisible by 3! It is a mathematical operation that returns the remainder of dividing i by 3. In a boolean context, this expression alone will resolve to false when i is divisible by 3!
Edit: I think you have a typo and just left out == 0.
edit: btw, i%3 bitshifted by 0 which doesn't change the number but because this is a math operation, it transforms it into a number in order to access the array with a number.
This bit's totally wrong but the rest is okay.
true in js is equivalent to 1, there's no functional need to "transform" it. the only reason to include the bitshift by 0 is so you're explicit in which bit the boolean is supposed to be setting.
TLDR: It uses the array as a lookup table and some bitwise operation magic to select which array entry is selected.
% in Java Script is the modulo operator thus i % 3 == 0 and i % 5 == 0 will be evaluated as true when i is divisible by 3 and 5 respectively.
<< is a left shift operator, when used with a Boolean the Boolean will be converted to a 1 or 0 (true or false) and then shifted left for x number of bits. For example True << 1 = 2 (ie. 10 in binary).
| is a bitwise OR operator. It evaluates a OR operation of each bit of two number.
For example 2 | 1 = 3 as in binary 10 | 01 = 11.
Putting it all together, (i % 3 == 0) << 0 represents the 20 bit (fizz) and (i % 5 == 0) << 1 represents the 21 bit (buzz). With the OR operator it will find when i is divisible by both 3 and 5.
Here's a table that might serve as a better view of how some numbers are evaluated:
i
Correct Array Index
Array Index (Binary)
Result
1
0
00
1
3
1
01
fizz
5
2
10
buzz
15
3
11
fizzbuzz
There are some parts of the code that are redundant though. Both << 0 and the final | 0 only serve to ensure that the result is a number, but << 0 doesn't modify the result and the OR operation between (i % 3 == 0) and (i % 5 == 0) will convert the result into a number making the final | 0 redundant.
Curious too, I have a theory but don't know js well enough. I think it's a nested ternary. The modulos are self explanatory, but I though << was a callback?
for(x=0; is cheaper than x=0;while(, so that's the best choice when using a loop structure.
I think that the top solutions (56 characters) must be using a different logical construct than the ternary string concatenation or the loop here, since there's no way to achieve savings on those as "wasted" characters like '': are syntactically required.
Maybe a recursive?
Edit:
Another note, ternary is not more efficient than logical and, once you include the <0 needed to force binary:
x%5&&'Buzz' // not the right condition
x%5||'Buzz' // spits out either 'Buzz' or the modulus, so the modulus has to be cleaned/ignored later
x%5?'':'Buzz'
x%5<1&&'Buzz' // not more space efficient than ternary
They've used the for loop's unused third condition to execute the print, and in the body assigned a value for Fizz which is then reused in the Buzz condition.
I posted my code golf for this elsewhere on this thread. The challenge is to just list the output from 1 to 100, but doesn't state in what order, so it's more efficient to list from 100 to 1:
I always thought this sub was about real-life examples of bad code, but it seems more like the majority of posts were made up by someone looking for internet likes.
It's bad because it's too clever. This example isn't the worst, but someone applying this kind of programming all over the place will create a big mess that will be hard to maintain.
I'd argue that this code is not bad. I have a very similar approach for displaying text depending on the enabled features:
const strings = [
gettext("X and Y are not available."),
gettext("X is available."),
gettext("Y is available."),
gettext("X and Y are available."),
];
el.textContent = strings[isFeatureEnabled('Y') << 1 | isFeatureEnabled('X')];
It's not bad code... He's trying to be over effective... Or moreover he's over engineering the modulos conditionals. It'll fail because those conditionals won't work like Case statements in Java...
75
u/slothordepressed Nov 14 '21
Can someone eli5 thks code? I know it works, but I would go with the old multiple ifs or a switch
32
u/--var Nov 14 '21
https://code.golf/fizz-buzz#javascript has the most efficient code (least characters) at 56 bytes. This code is 130 bytes. My best attempt is 62 bytes:
18
u/OrangeredStilton Nov 14 '21 edited Nov 14 '21
Image Transcription: JavaScript
[A screenshot of a small Mac Terminal window:]
I'm a human volunteer content transcriber for Reddit and you could be too! If you'd like more information on what we do and why we do it, click here!
17
u/noop_noob Nov 14 '21
Which part of this is bad?
4
u/propostor Nov 14 '21
I always thought this sub was about real-life examples of bad code, but it seems more like the majority of posts were made up by someone looking for internet likes.
3
u/FloweyTheFlower420 sadistic Nov 14 '21
"lets make branch prediction easier" moment
4
u/xiipaoc Nov 14 '21
That's not bad? Obviously don't put it in production, but that doesn't mean it's bad!
2
u/mech-me-feel Nov 19 '21
Back in April I actually did something like this in PHP! It didn't use bitshifts though. I did the following:
1
u/[deleted] Nov 15 '21
Now I really wanna try this with C and ternary operators
I think Iām gonna commit a warcrime for FizzBuzz lol
1
u/Tc14Hd Nov 15 '21
If you ever feel useless, remember that somebody wrote
<< 0.1
u/[deleted] Nov 15 '21
I'd argue that this code is not bad. I have a very similar approach for displaying text depending on the enabled features:
1
u/KevinChopra2019 Nov 14 '21
It's not bad code... He's trying to be over effective... Or moreover he's over engineering the modulos conditionals. It'll fail because those conditionals won't work like Case statements in Java...
0
u/[deleted] Nov 15 '21
[removed] ā view removed comment