r/programminghorror • • Jun 12 '25 • This 1

Javascript Javascript is filled with horror

Post image
2.4k Upvotes

337 comments sorted by

View all comments

9

u/Altruistic-Formal678 Jun 12 '25

Isn't there a website with a quizz full of stupid JS shit like this ? Like the result of Integer.parse(0.0000005) is 7 of stuff like this

5

u/look Jun 14 '25

It’d be a pretty simple quiz:

What happens when you pass the wrong type to this function? It casts it to a string.

That’s the explanation to all of the “crazy JS” posts — they didn’t read the documentation and they’re passing the wrong type.

2

u/Altruistic-Formal678 Jun 14 '25

Actually there is a parse function that take a decimal. But yes it does cast it to a string

1

u/CuAnnan Jun 14 '25

What does Integer.parse do?
I'll wait.

1

u/Altruistic-Formal678 Jun 14 '25

What it should do ? Mostly in this case, it is use to round number. But really it is not really used with decimals like this.

In this case: Integer.parse expect a string, so it parse 0.0000005 to string, which is "5.10e-7" and Integer.parse("5.10e-7") only takes the last character that is number, thus 7

1

u/CuAnnan Jun 14 '25

Integer.parse doesn't round.

That would be what Math.round is for.

It parses a string to an integer.

1

u/CuAnnan Jun 14 '25

Also, on reflection (and it's 00:15 here so I need a little latitude).

Integer.parse is java.

parseInt parses a String to an integer.

It doesn't round

parseInt(1.7) returns 1

1

u/Altruistic-Formal678 Jun 15 '25

It truncate, sorry, I mixed up. Also I never said I was a JS expert, I was just looking for the silly quizz