• kn0wmad1c
    link
    fedilink
    English
    arrow-up
    2
    arrow-down
    2
    ·
    edit-2
    6 months ago

    num % 2 isn’t a boolean result in any of these languages, so I feel like it would always output “odd”

    Edit: 0 is false, everything else is true.

    • moomoomoo309
      link
      fedilink
      English
      arrow-up
      8
      ·
      6 months ago

      All of those languages will convert numbers into booleans, 0 is false, all other numbers are true.

    • Sleepless One@lemmy.ml
      link
      fedilink
      English
      arrow-up
      5
      ·
      6 months ago

      In JS at least, there’s a concept of truthiness and falsiness. 0, undefined, null, and a few other non-boolean values are treated as false if used in conditionals and logical operations, while every other value is treated as true. I’m pretty sure python has something similar.

    • marcos@lemmy.world
      link
      fedilink
      arrow-up
      2
      ·
      6 months ago

      You’d be surprised.

      But seriously, numbers can be used as booleans in an impressive number of languages. Including machine code for almost every machine out there.