Beehaw
  • Communities
  • Create Post
  • search
    Search
  • Login
  • Sign Up
andioop ( andioop@programming.dev )  to Programming Horror@programming.devEnglish · 3 years ago

God I wish there was an easier way to do this

programming.dev

message-square
26
link
fedilink
117

God I wish there was an easier way to do this

programming.dev

andioop ( andioop@programming.dev )  to Programming Horror@programming.devEnglish · 3 years ago
message-square
26
link
fedilink
  • recursive_recursion [they/them] ( recursive_recursion@programming.dev ) 
    link
    fedilink
    English
    arrow-up
    7
    ·
    3 years ago

    modulo

    pseudocode:

    if number % 2 == 0
      return "number is even" (is_num_even = 1 or true)
    else
      return "number is odd" (is_num_even = 0 or false)
    

    plus you’d want an input validation beforehand

    • Mac ( mac@programming.dev ) 
      link
      fedilink
      arrow-up
      12
      ·
      3 years ago

      who needs modulo when you can get less characters out of

      while (number > 1) {
        number -= 2;
      }
      return number;
      

      very efficient

      edit: or theres the trusty iseven api

      • NullPointer ( nullPointer@programming.dev ) 
        link
        fedilink
        arrow-up
        6
        ·
        3 years ago

        here is somewhat less:

        return (number % 2) == 0;

      • perviouslyiner ( perviouslyiner@lemm.ee ) 
        link
        fedilink
        English
        arrow-up
        2
        ·
        3 years ago

        are the negative numbers all even?

        • I_am_10_squirrels ( I_am_10_squirrels@beehaw.org ) 
          link
          fedilink
          arrow-up
          2
          ·
          3 years ago

          Yes

    • avonarret1 ( avonarret1@programming.dev ) 
      link
      fedilink
      arrow-up
      4
      ·
      3 years ago

      🤦

    • Vex_Detrause ( Vex_Detrause@lemmy.ca ) 
      link
      fedilink
      arrow-up
      2
      ·
      3 years ago
      #You are an input. You have value! You matter!
      if number % 2 == 0
        return "number is even" (is_num_even = 1 or true)
      else
        return "number is odd" (is_num_even = 0 or false)
      

      Am I doing it right? /S.

      • PoolloverNathan ( PoolloverNathan@programming.dev ) 
        link
        fedilink
        English
        arrow-up
        4
        ·
        3 years ago

        Don’t put nbsps in code blocks, they show up literally.

    • RandomVideos ( RandomVideos@programming.dev ) 
      link
      fedilink
      arrow-up
      1
      ·
      3 years ago

      This code is terrible. If you input 10.66 it returns "number is odd

      It should be:

      if number % 2 == 0
        return "number is even" (is_num_even = 1 or true)
      else
        return "number is not even" (is_num_even = 0 or false)
      

Programming Horror@programming.dev

programming_horror@programming.dev

Subscribe from Remote Instance

Create a post
You are not logged in. However you can subscribe from another Fediverse account, for example Lemmy or Mastodon. To do this, paste the following into the search field of your instance: !programming_horror@programming.dev

Welcome to Programming Horror!

This is a place to share strange or terrible code you come across.

For more general memes about programming there’s also Programmer Humor.

Looking for mods. If youre interested in moderating the community feel free to dm @Ategon@programming.dev

Rules

  • Keep content in english
  • No advertisements (this includes both code in advertisements and advertisement in posts)
  • No generated code (a person has to have made it)

Credits

  • Icon base made by Lorc under CC BY 3.0 with modifications to add a gradient
Visibility: Public
globe

This community can be federated to other instances and be posted/commented in by their users.

  • 1 user / day
  • 1 user / week
  • 1 user / month
  • 21 users / 6 months
  • 23 local subscribers
  • 2.4K subscribers
  • 35 Posts
  • 125 Comments
  • Modlog
  • mods:
  • Vacant ( Vacant@programming.dev ) 
  • BE: 0.19.15
  • Modlog
  • Legal
  • Instances
  • Docs
  • Code