• danda@lemmy.zip
    link
    fedilink
    arrow-up
    2
    ·
    1 month ago

    It’s because parseInt is expecting a string, so the decimal gets converted to a string, and 0.0000005.toString() returns 5e-7.

    • towerful@programming.dev
      link
      fedilink
      arrow-up
      1
      ·
      1 month ago

      Yup. parseInt is for strings.
      Math.floor, Math.ceil, Math.round or Math.trunc are for numeric type “conversions” (cause its still a float)

      • mmddmm@lemm.ee
        link
        fedilink
        arrow-up
        0
        arrow-down
        1
        ·
        edit-2
        1 month ago

        Nah, it’s stupid either way.

        “5e-7” is not an int to be parsed. Neither is “0.5”.

        • ThirdConsul@lemmy.ml
          link
          fedilink
          arrow-up
          1
          ·
          edit-2
          1 month ago

          Ah, folly of untyped systems. Tbh this behaviour makes sense given the rules implemented within the language. Anything passed to parseInt is casted to string and then parsed.

          Is it shitty behaviour - yes. Does it make sense in given the language implementation - yes.

        • LeninOnAPrayer@lemm.ee
          link
          fedilink
          English
          arrow-up
          1
          ·
          edit-2
          1 month ago

          People give JS a lot of shit. And I do too. But it’s meant to continue running and not fail like C code would. It’s meant to basically go “yeah, sure I’ll fuck with that” and keep trucking.

          So you can always make it do stupid shit when you use it a stupid way.

          Is this bad? Maybe. Was it the intention of the language? Absolutely.

          Typescript fixes a lot of these headaches. But I feel like JS is doing exactly what it was meant to do. Keep trucking even when the programmer asks it to do stupid shit.

          If you’re using JS and don’t understand this then it’s your fault and not the languages fault.

          Do we all want to live in a world of typedefs as strict as C and have our webpages crash with the slightest unexpected char input? Probably not.

          We don’t notice all the time JS goes “yeah I can fuck with that” and it works perfectly. We only notice the times it does that and it results in something silly.

          TLDR: JS does what it was made to do. And because of that it looks absolutely ridiculous sometimes.

    • Victor@lemmy.world
      link
      fedilink
      arrow-up
      1
      ·
      1 month ago

      Classic people who don’t know how to code wat. Passing a number in place of a string argument because they don’t know what they’re doing.

      • jjjalljs@ttrpg.network
        link
        fedilink
        arrow-up
        0
        ·
        1 month ago

        Javascript could throw an error to alert you that the input is supposed to be a string, like most languages would do.

        • Victor@lemmy.world
          link
          fedilink
          arrow-up
          0
          ·
          edit-2
          1 month ago

          But you’re calling a function specifically made for passing a string to an int… 😆 There’s gotta be some common sense somewhere here, guys.

          Still, it’s a very good point. JS should do this.

          I would suspect one reason it doesn’t do this is to be backwards compatible.

        • heavy@sh.itjust.works
          link
          fedilink
          English
          arrow-up
          0
          ·
          1 month ago

          Theoretically, Javascript is an untyped language, so there aren’t supposed to really be static types. Giving type errors in this situation would be against design.

          • bleistift2@sopuli.xyz
            link
            fedilink
            English
            arrow-up
            0
            ·
            1 month ago

            JavaScript has types and it does have type errors, for instance

            > null.foo
            Uncaught TypeError: null has no properties
            

            Please stop spouting nonsense on issues you know nothing about.

            • heavy@sh.itjust.works
              link
              fedilink
              English
              arrow-up
              0
              arrow-down
              1
              ·
              1 month ago

              Dynamic types aren’t static types my man. I think you got some learning to do.