Hmm... As a bootcamp student, I'm trying to untangle this.
[!(offsetIndex % 3) + 0]
I see this checks the modulus, and inverts the result. Any non-zero int is truthy, and this expression makes it false . . . +0 to coerce the false to an int. That is enough that the entire thing evaluates falsy, which then results in outputting offsetIndex on the otherside of the or. I had to drop this in a node repl to follow it, but I eventually got it š
But what is the ["", "Fizz"][!(offsetIndex % 3) + 0] double-array looking thing there? I thought it was a 2d array at first, but that doesn't seem right for a number of reasons.
I'm pretty sure the first pair of square brackets creates an array, and the second one indexes into that array. So I think they are array indexing into the first array with either 0 or 1 to pick the empty string or "Fizz" depending on the offsetIndex!
yup, it defines the array first const array = ["", "Fizz"] and then access the index array[!(offsetIndex % 3) + 0]. The expression will resolve either to true+0 -> 1 or false+0 -> 0
I THOUGHT it might have been something like that, but I was thinking about it wrongly . . . I wasn't thinking of it as the array followed by the index, I was thinking of it as a variable. So ["an", "array"] was the name of the array, and then it had it's own index. Not very practical.
But the actual use is quite cool and makes plenty sense.
Thanks!
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Hmm... As a bootcamp student, I'm trying to untangle this.
[!(offsetIndex % 3) + 0]
I see this checks the modulus, and inverts the result. Any non-zero int is truthy, and this expression makes it false . . .
+0
to coerce thefalse
to an int. That is enough that the entire thing evaluates falsy, which then results in outputtingoffsetIndex
on the otherside of theor
. I had to drop this in a node repl to follow it, but I eventually got it šBut what is the
["", "Fizz"][!(offsetIndex % 3) + 0]
double-array looking thing there? I thought it was a 2d array at first, but that doesn't seem right for a number of reasons.I'm pretty sure the first pair of square brackets creates an array, and the second one indexes into that array. So I think they are array indexing into the first array with either 0 or 1 to pick the empty string or "Fizz" depending on the offsetIndex!
Hope that helps!
yup, it defines the array first
const array = ["", "Fizz"]
and then access the indexarray[!(offsetIndex % 3) + 0]
. The expression will resolve either totrue+0 -> 1
orfalse+0 -> 0
holy shit. that's cool.
I THOUGHT it might have been something like that, but I was thinking about it wrongly . . . I wasn't thinking of it as the array followed by the index, I was thinking of it as a variable. So
["an", "array"]
was the name of the array, and then it had it's own index. Not very practical.But the actual use is quite cool and makes plenty sense.
Thanks!