this post was submitted on 16 Jun 2023
52 points (93.3% liked)

Programmer Humor

32425 readers
923 users here now

Post funny things about programming here! (Or just rant about your favourite programming language.)

Rules:

founded 5 years ago
MODERATORS
 
you are viewing a single comment's thread
view the rest of the comments
[–] TwilightKiddy 1 points 1 year ago

parseInt() takes string as an input. From first character, it goes on till it hits a non-digit character, and then converts resulting string to an integer. JS is not strictly-typed, so, when I feed it a floating point number, it implicitly converts it to string. Things like 0.01 it converts like "0.01", no problem here, our first character is zero, and then there is a dot, that's not a digit, so we parse "0" to integer and get our zero. But at some point it switches to scientific notation when converting to string, so, our 0.0000001 becomes "1E-7". Then we take one as our first character, stop at E as it's not a digit and we get "1" parsed to one. Praise the loosly-typed hell.