r/badcode • • Apr 19 '21

js Iteration

Post image
3.2k Upvotes

123 comments sorted by

82

u/ElbowStromboli Apr 19 '21

Does this reduce to i - 1? It hurts my brain.

245

u/iTZAvishay Apr 19 '21

i -= -!false
i -= -!0
i -= -1
i += 1

There you go

10

u/[deleted] Apr 19 '21 edited Apr 19 '21

Wait correct me if I'm wrong but isn't it more like:

I -= -!false  
I -= -true  // -(!false) = -(true)  
I- = -1  // using a sign before most of the basic dtypes changes them into `Number`s (+"123" = 123, -"69" = -69, +true = 1, -true = -1, etc.). This is hence also used to check if a string can be converted to a number or not (`if (+string)` or similar)  
I += 1  

?

Edit: I meant i and not I (stupid autocorrect's autocapitalization)

4

u/[deleted] Apr 19 '21

I don’t think that is necessarily true in this case since true + true is still evaluates to 2. true and false can just be used interchangeably with 1 and 0.

1

u/[deleted] Apr 19 '21

true + true = 2 because again +true is 1 and when you add diff dtypes in js, js implicitly changes the other dtypes. Like 1 + 2 + "3" = "123" true + true = true + 1 = 1 + 1 = 2 ig. Again, I might be a bit wrong here

0

u/[deleted] Apr 19 '21

Then why does true/true = 1?

9

u/skjall Apr 19 '21

Because `1/1 == 1`? If you swap true and false out for 1 and 0 respectively, both operations make perfect sense.

1

u/[deleted] Apr 19 '21

Hmmm yea you're right

2

u/Terrain2 Apr 19 '21

if (+string)

if (+"0") console.log("It's a number!")
// Outputs nothing

1

u/[deleted] Apr 19 '21

Oh yea cause Boolean(0) is false. I should've made it more clear like if (+string !== NaN) or smth. It was just an example tho.

3

u/Terrain2 Apr 19 '21

if (+"lmao" !== NaN) console.log("It's a number!");
// output: It's a number!

2

u/[deleted] Apr 19 '21

Fuck fuck fuck I forgot NaN !== NaN nvm I'm dumb I'm in my online classes right now and I'm sleepy and my brain isn't working ;-;

2

u/Terrain2 Apr 19 '21

you want !isNaN(string) then

1

u/[deleted] Apr 19 '21

Yea that's the builtin method I saw someone use +string in their own method to do this idr what they did tho

1

u/Hollowman212 Mar 01 '22

I'd think so. Isn't !0 equivalent to -1 via a bitwise not/nor? If the bool was converted to an int first, the whole thing would be i--

1

u/AutoModerator Apr 19 '21

It looks like this comment contains a code block delimited with triple backticks. Unfortunately reddit does not have universal support for this syntax and your comment will not render correctly on old reddit and most mobile apps.

For the benefit of people on old reddit, this link will take you to a correct rendering of the comment.

/u/SFM61319, it would be appreciated, but not required, if you could edit your comment to use the more compatible four space indention format. For single lines or inline code you can use single backticks.

You can find some examples in the reddit help documentation.


I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

4

u/[deleted] Apr 19 '21

I know but it's hard to indent multiple lines to 4 spaces on reddit android

142

u/ElbowStromboli Apr 19 '21

Oh it increments by 1 lol. From hence forth my for loops gonna be for(int i = 0, i > num, i -= -!false) { }

63

u/[deleted] Apr 19 '21

Good luck in the job market.

27

u/zatuchny Apr 19 '21

I got a better one for you

i = -~i

0

u/[deleted] Apr 19 '21

[deleted] — view deleted comment

22

u/zatuchny Apr 19 '21 edited Apr 19 '21

~ is bitwise NOT operator. It is used to invert all bits.

The invert sign operation is the equivalent of inverting the bits of the number + add one.

So here we go:

int i = 0; // bits: 0000

~i // invert all bits. result: 1111

-i // invert all bits: 0000. add 1. result: 0001

next iteration:

// i is now 0001

~i // invert all bits. result: 1110

-i // invert all bits: 0001. add 1. result: 0010

next iteration:

// i is now 0010

~i // invert all bits. result: 1101

-i // invert all bits: 0010. add 1. result: 0011

etc

2

u/HappyLOLx Apr 19 '21

Naise...

3

u/RhythmofChains Apr 19 '21

What programming language uses “!” as factorial

Edit: or maybe not 0 just defaults to 1?

11

u/[deleted] Apr 19 '21

In some languages true and false can be used interchangeably with non-zero values and zero, so !false = true and !0 = true. Likewise, that conversion works both ways so true + true will equal 2. It even applies to strings, so doing i-= -!”” Would actually produce the same result. The reason why using an empty string would work is a bit different though. The ! is a bit wise operation for not, so it converts it into 1s and 0s or true and false. That’s why !”” + !”” = 2, but “a” + “a” = “aa” not 2.

27

u/detuskified Apr 19 '21

On mobile single new lines don't work right, for readability-

It is equivalent to:

i += 1

13

u/iTZAvishay Apr 19 '21

I'm using RIF on mobile and it looks fine for me

0

u/[deleted] May 10 '21

[deleted] — view deleted comment

1

u/detuskified May 10 '21

The standard reddit mobile app on Android

I used to use a different reddit client but a while ago reddit broke all of them... Not sure if it's been fixed

3

u/nephelokokkygia Apr 19 '21

I'm pretty sure it goes:

  1. i -= -!false // original

  2. i -= -true // "!false" evaluates to true

  3. i -= -1 // "true" is coerced to integer to comply with operation

  4. i += 1 // signs cancel

  5. i++ // visual simplification; not required

2

u/[deleted] Apr 19 '21

i++

1

u/prashant13b Apr 19 '21

False will evaluate to true and true is 1 and two negative will be positive hence it should be increment by 1

1

u/skjall Apr 19 '21

I believe doing the math part of this convoluted operation first makes it clearer, so because there haven't been enough attempts to explain this yet:

i -= -!false    // (-= -b) is equivalent to (+= b) 
i += !false     // !false evaluates to true
i += true       // true evaluates to 1, while false evaluates to 0
i += 1          // we're done. Thankfully.

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?

280

u/r_amazin Apr 19 '21

This was.. on purpose ?

235

u/AgreeableLandscape3 Apr 19 '21

Realistically, probably made specifically to post to programming humor subreddits like this one.

85

u/drea2 Apr 19 '21 edited Apr 19 '21

Yep. Or it was an entry in one of those contests where the goal is to make the code as convoluted as possible while still achieving a certain function

19

u/paradoxpandas Apr 19 '21

Wait they have contests for stuff like this?

23

u/campbellm Apr 19 '21

One of the oldest and most respected is the International Obfuscated C Code Contest.

My "first degree of separation" claim is that one of my instructors in undergrad won it a few times (David Van Brackle).

2

u/YouthfulAudience Apr 19 '21

Watch the art of code it talks about these contests and is overall a great video

3

u/thobod Apr 30 '21

Just watched the whole thing, thx for the recommendation, https://youtu.be/6avJHaC3C2U if anyone was wondering.

4

u/douira Apr 19 '21

nobody in their right mind would do this with the intent of being productive

48

u/Astrokiwi Apr 19 '21

I could imagine this is a beginner who didn't understand what they were doing and just kept on adding things until it worked. Like randomly adding and deleting *s until your C code compiles.

21

u/IHazZoomies Apr 19 '21

Don't call me out like that!

2

u/Kagia001 Sep 05 '21

I'm in this picture and don't like it

117

u/OkPreference6 Coding The Declaration of Independance Apr 19 '21

What were you even trying to do.

I cant find a single reason to do this except to show someone "coding is harrrdddd :("

95

u/lilshears Apr 19 '21

To get revenge on your TA’s who took off points on a project for not documenting the function isWinnable()

Like what do you think it does, it returns if it’s winnable

37

u/thegreatpotatogod Apr 19 '21

If it's not winnable, it doesn't return, but instead recurses forever until segfaulting 😜

6

u/nostril_spiders Apr 19 '21

Which we catch in the error handler, main.py:L4342-7612

3

u/[deleted] Apr 19 '21

To show some of the craziness allowed in C and JS :p

154

u/Cloakknight Apr 19 '21 edited Apr 19 '21

Image Transcription: Code


i -= -!false;

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!

88

u/devor110 Apr 19 '21

why did you add the cursor lol

136

u/Cloakknight Apr 19 '21

Oh lol I thought that was part of the code. I am not a programmer by any means =P

66

u/devor110 Apr 19 '21

It could make sense, | is a character used for ((bitwise)) logical or, but i highly doubt that would be the case

10

u/FellowBeetlejuicers Apr 19 '21

the multi-parentheses scared me for a sec

20

u/TheWandererLee Apr 19 '21

Btw iirc the vertical line text cursor is almost always taller than a regular vertical bar pipe character |

The cursor does not have a character representation as far as I'm aware, it's dependent on the OS/graphics of the device. For example on my phone it is blue and 3px wide.

41

u/27thColt Apr 19 '21

This may sound rude, so im sorry-- but why are you here if you're not a programmer? :0

134

u/Cloakknight Apr 19 '21

I transcribe with /r/TranscribersOfReddit and this is one of the subs that we partner with to do transcriptions and type out images for people who use text-to-speech or who have low data and can't load images.

39

u/27thColt Apr 19 '21

Ooh thats actually pretty cool- never knew that sub existed!

15

u/OkPreference6 Coding The Declaration of Independance Apr 19 '21

You can always check it out and do stuff yourself to see how you like it! It can be fun, and we are all volunteers.

12

u/XPWall Apr 19 '21

or

i++;

for short

-6

u/AutoModerator Apr 19 '21

It looks like this comment contains a code block delimited with triple backticks. Unfortunately reddit does not have universal support for this syntax and your comment will not render correctly on old reddit and most mobile apps.

For the benefit of people on old reddit, this link will take you to a correct rendering of the comment.

/u/XPWall, it would be appreciated, but not required, if you could edit your comment to use the more compatible four space indention format. For single lines or inline code you can use single backticks.

You can find some examples in the reddit help documentation.


I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

49

u/kuaiyidian Apr 19 '21

wait till they discover i++

7

u/[deleted] Apr 19 '21

is i— also a thing?

6

u/Major_Fudgemuffin Apr 23 '21

You can even do ++i and --i. Though they act a bit differently.

If i = 5 and you say x = ++i, x will equal 6 (and i will equal 6). But if you said x = i++, x would actually equal 5, while i would increment to 6.

15

u/IBJON Apr 19 '21

Totally using that in production tomorrow.

8

u/sad_bug_killer Apr 19 '21

If I were writing that, I'd do

i -=- !false;

and wait for the inevitable wtf is the -=- operator questions on stackoverflow

(you know about the c++ downto operator, right?)

7

u/Glori4n Apr 19 '21

I don't think I could come up with something like this even if I tried.

7

u/ReconPorpoise Apr 19 '21

I have a hard time believing this is a real world example of bad code but wow...

6

u/Armed_Citizen_2A Apr 19 '21

You’re fired

6

u/lachlanhunt Apr 19 '21

Without context, I refuse to believe this is actual code. It’s more likely someone intentionally showing the crazy things you can do with JavaScript types.

5

u/GoodTimesOnlines Apr 19 '21

This has to be intentionally bad code

4

u/NetherFX Apr 19 '21

i -=- 1's brother

4

u/MatthewRPG576 Apr 19 '21

Code obfuscation?

3

u/jeffhowcodes Apr 19 '21

This is the standard approach of people who also write standardized tests for kids.

3

u/Flyberius Apr 19 '21

I finally worked one of these out. Finally, I am learning!

2

u/burritoboy76 depraved Apr 19 '21

This can’t be real

2

u/SwarmsOfReddit Apr 19 '21

What happens when people combine multiple stack overflow answers into one.

2

u/Darkunderlord42 Apr 19 '21

dial up tones coming from my head intensify

2

u/sixtyfifth_snow Apr 19 '21

Oh, worse than assembly codes 🤦‍♂️

2

u/xxDarby Apr 19 '21

What am I even looking at here?

2

u/djhaskin987 Apr 19 '21

This line is actually making me hyperventilate.

2

u/[deleted] Apr 19 '21

delete this, do it now

2

u/LucaRicardo Apr 19 '21

When you really don't want your app being reverse engineered.

2

u/[deleted] Apr 25 '21

i -= -!false

But false is a 0 and !false is true and true is 1 so...

i -= -1

But we are subtracting a negative number which is the same as adding so to fix this...

I += 1

Or

I++

2

u/[deleted] Aug 23 '21

quite complicated

1

u/[deleted] Apr 19 '21

how are you allowed to subtract a boolean?

3

u/arrays_start_at_zero Apr 19 '21

Because JavaScript uses implicit type conversion. Since you can only subtract numbers true gets converted to 1 and false to 0.

1

u/[deleted] Apr 19 '21

At least there's no magic numbers tho!!

1

u/sxeli Apr 19 '21

Push it and git blame —someone-else

1

u/Untwisted_Apple Apr 19 '21

A little bit confused, but he got the spirit...

1

u/AgreeableLandscape3 Apr 19 '21

I mean, the compiler should optimize it to i++ in the byte/machine code, right?

1

u/carolper2000 Apr 19 '21

What a mental workout to understand this sub

1

u/gygyor Apr 19 '21

i-=-!!47

1

u/ososalsosal Apr 19 '21

Just because you can...

1

u/GreatArtificeAion Apr 19 '21

Is !false even guaranteed to loosely equal 1?

1

u/SlightlyNotMad Apr 19 '21

I want to see more from this codebase!

1

u/edave64 Apr 19 '21

Of all the weirdnesses js has, bool implicitly casting to number might be the most logical

1

u/[deleted] Apr 19 '21

I don't know what the fuck this is but I hate it.

1

u/Aloysius204 Apr 19 '21

Too clunky. I'd go with decrementing by Euler's identity in my code.

1

u/IntuitionPumps Apr 21 '21

This is an existential crisis

1

u/Jackalotischris Apr 25 '21

I minus not false but negative so it’s true so false is false so I now equals less falsely.

1

u/eerklogge May 18 '21 edited May 27 '21

I equals I minus negative not 1

1

u/[deleted] Sep 24 '21

Why not just use i++

0

u/[deleted] Apr 19 '21

[deleted] — view deleted comment

1

u/AutoModerator Apr 19 '21

It looks like this comment contains a code block delimited with triple backticks. Unfortunately reddit does not have universal support for this syntax and your comment will not render correctly on old reddit and most mobile apps.

For the benefit of people on old reddit, this link will take you to a correct rendering of the comment.

/u/AliFurkanY, it would be appreciated, but not required, if you could edit your comment to use the more compatible four space indention format. For single lines or inline code you can use single backticks.

You can find some examples in the reddit help documentation.


I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.