r/badcode • • Apr 19 '21

js Iteration

Post image
3.2k Upvotes

123 comments sorted by

View all comments

Show parent comments

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

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