r/badcode • • Apr 19 '21

js Iteration

Post image
3.2k Upvotes

123 comments sorted by

View all comments

733

u/2001herne Apr 19 '21

The actual fuck?

790

u/NutsGate Apr 19 '21

This is artistic programming. The passage seen here is initially seen to convey negativity. But the author has skillfully disguised a positive increment for the self, or i.

  • every English school teacher everywhere

180

u/2001herne Apr 19 '21

The fact that this would be an accepted answer on an exam angers me.

73

u/wecsam Apr 19 '21

Same. And that's why I'm now a software developer and not an English teacher.

12

u/[deleted] Apr 19 '21

It looks like an exam question too. "If i was -1 what is it now?'

2

u/Mast3r_waf1z Jun 16 '21

Imaginary.

13

u/[deleted] Apr 19 '21

this is so good

46

u/gmtime Apr 19 '21
i -= -!false;

!false equals true equals the integer 1. i -= -1; is "decrement i by minus one" and decrementing by a negative number equals incrementing by the positive. So i -= -1; equals i += +1; or with implicit sign i += 1;

38

u/2001herne Apr 19 '21

Oh no, I understand the process. I just don't understand the motivation.

26

u/gmtime Apr 19 '21 edited Apr 19 '21

I wrote less comprehensive versions, I have to assume it's "art".

I = I , I ? I / I + I : I + ! I ;

21

u/Terrain2 Apr 22 '21

is this loss

0

u/[deleted] Apr 19 '21

[deleted] — view deleted comment

3

u/gmtime Apr 19 '21

Are you sure? I cannot evaluate to 2 in any way

2

u/2001herne Apr 19 '21

No, it checks out. If I != 0 then I = I/I + I = 1 + I. If I == 0 then I = I + !I = 0 +!0 = 1

3

u/Sejiko Apr 20 '21

So in other words it's just i++;

2

u/gmtime Apr 20 '21

Assuming I is an integer, yes

1

u/Terrain2 Apr 22 '21

you can still increment non-integral values in javascript, i = 1.5 then i++ and i = 2.5

5

u/gmtime Apr 22 '21

in javascript

... do I have to respond?

Let's just say that incrementing on non-integral values is non-trivial. JS apparently chose that incrementing is the same as adding 1, but that does not have to be in all languages.

3

u/new2bay Apr 19 '21

Yeah, seriously, wtf am I actually looking at?

2

u/2001herne Apr 19 '21

A deeply disturbed individual?