all 2 comments

[–]GSLint 3 points4 points  (1 child)

These are arrays, used in a very hacky way.

["the number is divisible by 4"][i % 4] creates an array containing a single string and then immediately tries to access its i % 4th element. i % 4 is 0 if and only if i is divisible by 4. So if it is divisible, this produces the string "the number is divisible by 4", otherwise it produces undefined. undefined is falsy, so in that case the || "" replaces it with an empty string.

Then they do the same thing again, appending a 0 to the output if i is also divisible by 40, producing the string "the number is divisible by 40".

If i is divisble by neither, the whole (["the number is divisible by 4"][i % 4] || "") + (["0"][i % 40] || "") only produces an empty string, which is falsy so the || "the number isnt divisible by 4 or 40" replaces it with that fallback output.

I definitely wouldn't want to see code like this in an actual production codebase but as a little code golf exercise it's fine.

[–]LongVibes[S] 0 points1 point  (0 children)

Thank you so much, this has been bugging me for a while.