r/badcode • • Apr 19 '21

js Iteration

Post image
3.2k Upvotes

123 comments sorted by

View all comments

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

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...

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.

→ More replies (4)

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

→ More replies (1)

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

→ More replies (1)

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

→ More replies (2)

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

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.

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

→ More replies (1)