Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. The Weird and The Wonderful
  4. That's one way to do it

That's one way to do it

Scheduled Pinned Locked Moved The Weird and The Wonderful
comhelp
64 Posts 38 Posters 63 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A atverweij

    That can be done better with a recursive function:

    function IsEven(number) {
    if (number === 1 return false;
    elseif (number === 2) return true;
    else return IsEven(number - 2);
    }

    Now it does all positive numbers :laugh: EDIT: because I get serious reactions on this code The code above is how NOT to do it - it's satire.

    B Offline
    B Offline
    BruceCarson
    wrote on last edited by
    #33

    No...only positive integers. Since this is JS, some jerk could pass a float in and get your recursive function to march off the negative end of the numbers.

    A 1 Reply Last reply
    0
    • B BruceCarson

      No...only positive integers. Since this is JS, some jerk could pass a float in and get your recursive function to march off the negative end of the numbers.

      A Offline
      A Offline
      atverweij
      wrote on last edited by
      #34

      Hmmm, you are right. The the improved improved version.

      function IsEven(number) {
      if (number === 1) return false;
      else if (number ===2) return true;
      else if (number > 2) return IsEven(number - 2);
      else return IsEven(number + 2)
      }

      Now it does negatives too ;P EDIT: because I get serious reactions on this code The code above is how NOT to do it - it's satire.

      B K 2 Replies Last reply
      0
      • J John Wellbelove

        They've missed a whole set of entries for those that speak British style English. American: Thirty-Two Thousand One Hundred Twelve British: Thirty-Two Thousand One Hundred And Twelve I'd better get editing...

        M Offline
        M Offline
        MarkTJohnson
        wrote on last edited by
        #35

        The AND denotes the decimal or fractional portion of the number. So Brits in the example above, Twelve whats?

        I’ve given up trying to be calm. However, I am open to feeling slightly less agitated.

        Richard DeemingR J 2 Replies Last reply
        0
        • A atverweij

          Hmmm, you are right. The the improved improved version.

          function IsEven(number) {
          if (number === 1) return false;
          else if (number ===2) return true;
          else if (number > 2) return IsEven(number - 2);
          else return IsEven(number + 2)
          }

          Now it does negatives too ;P EDIT: because I get serious reactions on this code The code above is how NOT to do it - it's satire.

          B Offline
          B Offline
          BruceCarson
          wrote on last edited by
          #36

          And now this line of code is used: IsEven (4.2); It'll bounce around 0 until the heat death of the universe.

          A 1 Reply Last reply
          0
          • A atverweij

            Hmmm, you are right. The the improved improved version.

            function IsEven(number) {
            if (number === 1) return false;
            else if (number ===2) return true;
            else if (number > 2) return IsEven(number - 2);
            else return IsEven(number + 2)
            }

            Now it does negatives too ;P EDIT: because I get serious reactions on this code The code above is how NOT to do it - it's satire.

            K Offline
            K Offline
            kalberts
            wrote on last edited by
            #37

            Why are all the 'else's there? Do they improve code generation?

            A 1 Reply Last reply
            0
            • M MarkTJohnson

              The AND denotes the decimal or fractional portion of the number. So Brits in the example above, Twelve whats?

              I’ve given up trying to be calm. However, I am open to feeling slightly less agitated.

              Richard DeemingR Offline
              Richard DeemingR Offline
              Richard Deeming
              wrote on last edited by
              #38

              Not in British English (or just "English", as it's known). :) "One hundred and twelve" === 112, not 100.12.


              "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

              "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

              1 Reply Last reply
              0
              • P PIEBALDconsult

                function isOdd(x) { return isEven(x**-1**); } ? function isOdd(x) { return isEven(**~**x); } ?

                J Offline
                J Offline
                jsc42
                wrote on last edited by
                #39

                PIEBALDconsult wrote:

                function isOdd(x) { return isEven(x**-1**); }

                The sad thing is that that wouldn't work when testing against '1' as that would be testing to see if 0 was even and their isEven does not have a test for 0.

                1 Reply Last reply
                0
                • M MarkTJohnson

                  The AND denotes the decimal or fractional portion of the number. So Brits in the example above, Twelve whats?

                  I’ve given up trying to be calm. However, I am open to feeling slightly less agitated.

                  J Offline
                  J Offline
                  John Wellbelove
                  wrote on last edited by
                  #40

                  The British English convention is to put an 'and' between any hundreds and non-zero tens/units and 'point' to indicate the fraction part. Therefore 123,456.789 is... "One hundred and twenty three thousand, four hundred and fifty six point seven, eight, nine"

                  1 Reply Last reply
                  0
                  • B Bernhard Hiller

                    And 0. Is 0 even or odd? How can we know when that code is lacking the answer to this most important question?

                    Oh sanctissimi Wilhelmus, Theodorus, et Fredericus!

                    D Offline
                    D Offline
                    Daniel Pfeffer
                    wrote on last edited by
                    #41

                    doesn't it throw an exception? :-\

                    Freedom is the freedom to say that two plus two make four. If that is granted, all else follows. -- 6079 Smith W.

                    1 Reply Last reply
                    0
                    • M MarkTJohnson

                      I'm trying to guess what certain CPers would do if they were confronted with this person. JSOP - bullet in the guy's head to put both the guy and everyone else out of their misery. honey the codewitch - After she quit twitching, she'd write a parser to optimize the code. OriginalGriff - figures out how to turn it into a CCC clue Nagy - just wanders off, asking where the gin is. Me - I'd tell him to turn it all into a switch statement.

                      I’ve given up trying to be calm. However, I am open to feeling slightly less agitated.

                      D Offline
                      D Offline
                      Daniel Pfeffer
                      wrote on last edited by
                      #42

                      Me: Have Security dress the guy in a hazmat suit, and escort him out of the building. I'd then cordon off the area and decontaminate everything he touched on his visit - in an autoclave, by choice. This level of stupidity leaves traces wherever it goes.

                      Freedom is the freedom to say that two plus two make four. If that is granted, all else follows. -- 6079 Smith W.

                      J 1 Reply Last reply
                      0
                      • Richard DeemingR Richard Deeming

                        Someone's already beaten you to that suggestion: [Feature Request] Support for British English "and" · Issue #85 · samuelmarina/is-even · GitHub[^] :-D


                        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                        N Offline
                        N Offline
                        Nelek
                        wrote on last edited by
                        #43

                        Now the question is... Is the one with that request trolling him? Or does he really needs it for his usage?

                        M.D.V. ;) If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about? Help me to understand what I'm saying, and I'll explain it better to you Rating helpful answers is nice, but saying thanks can be even nicer.

                        Richard DeemingR K 2 Replies Last reply
                        0
                        • N Nelek

                          Now the question is... Is the one with that request trolling him? Or does he really needs it for his usage?

                          M.D.V. ;) If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about? Help me to understand what I'm saying, and I'll explain it better to you Rating helpful answers is nice, but saying thanks can be even nicer.

                          Richard DeemingR Offline
                          Richard DeemingR Offline
                          Richard Deeming
                          wrote on last edited by
                          #44

                          Well, my comment on that issue requesting support for the Yan tan tethera[^] counting system was perfectly legitimate. :laugh:


                          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                          "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                          1 Reply Last reply
                          0
                          • B BruceCarson

                            And now this line of code is used: IsEven (4.2); It'll bounce around 0 until the heat death of the universe.

                            A Offline
                            A Offline
                            atverweij
                            wrote on last edited by
                            #45

                            You are taking this code seriously?

                            K 1 Reply Last reply
                            0
                            • K kalberts

                              Why are all the 'else's there? Do they improve code generation?

                              A Offline
                              A Offline
                              atverweij
                              wrote on last edited by
                              #46

                              No, they worsen it :-\ :-\

                              1 Reply Last reply
                              0
                              • A atverweij

                                That can be done better with a recursive function:

                                function IsEven(number) {
                                if (number === 1 return false;
                                elseif (number === 2) return true;
                                else return IsEven(number - 2);
                                }

                                Now it does all positive numbers :laugh: EDIT: because I get serious reactions on this code The code above is how NOT to do it - it's satire.

                                B Offline
                                B Offline
                                Bernhard Hiller
                                wrote on last edited by
                                #47

                                With big number (I mean really big numbers), you will run into a StackOverflowException. What about "tail recursion"? :-D

                                Oh sanctissimi Wilhelmus, Theodorus, et Fredericus!

                                A 1 Reply Last reply
                                0
                                • B Bernhard Hiller

                                  With big number (I mean really big numbers), you will run into a StackOverflowException. What about "tail recursion"? :-D

                                  Oh sanctissimi Wilhelmus, Theodorus, et Fredericus!

                                  A Offline
                                  A Offline
                                  atverweij
                                  wrote on last edited by
                                  #48

                                  Really? :) :laugh: :-\

                                  1 Reply Last reply
                                  0
                                  • K Kent Sharkey

                                    GitHub - samuelmarina/is-even[^]

                                    Quote:

                                    This is a 100% serious project, and it is made to help the community.

                                    A brief excerpt:

                                    function isEven(number) {
                                    if(number === 1) return false;
                                    else if(number === 2) return true;
                                    else if(number === 3) return false;
                                    else if(number === 4) return true;
                                    else if(number === 5) return false;
                                    else if(number === 6) return true;
                                    else if(number === 7) return false;

                                    They're up to 1,000,001 now. Help them grow! :doh:

                                    TTFN - Kent

                                    1 Offline
                                    1 Offline
                                    11917640 Member
                                    wrote on last edited by
                                    #49

                                    100% this is a joke, and I like it.

                                    1 Reply Last reply
                                    0
                                    • K Kent Sharkey

                                      GitHub - samuelmarina/is-even[^]

                                      Quote:

                                      This is a 100% serious project, and it is made to help the community.

                                      A brief excerpt:

                                      function isEven(number) {
                                      if(number === 1) return false;
                                      else if(number === 2) return true;
                                      else if(number === 3) return false;
                                      else if(number === 4) return true;
                                      else if(number === 5) return false;
                                      else if(number === 6) return true;
                                      else if(number === 7) return false;

                                      They're up to 1,000,001 now. Help them grow! :doh:

                                      TTFN - Kent

                                      G Offline
                                      G Offline
                                      GuyThiebaut
                                      wrote on last edited by
                                      #50

                                      At first I thought "Dang! I am looking for a package that determines if a number is odd." But it actually turns out that they have helpfully written one. Here it is if anyone else needs it -> odd numbers[^] A real godsend!

                                      “That which can be asserted without evidence, can be dismissed without evidence.”

                                      ― Christopher Hitchens

                                      1 Reply Last reply
                                      0
                                      • D Daniel Pfeffer

                                        Me: Have Security dress the guy in a hazmat suit, and escort him out of the building. I'd then cordon off the area and decontaminate everything he touched on his visit - in an autoclave, by choice. This level of stupidity leaves traces wherever it goes.

                                        Freedom is the freedom to say that two plus two make four. If that is granted, all else follows. -- 6079 Smith W.

                                        J Offline
                                        J Offline
                                        Jorgen Andersson
                                        wrote on last edited by
                                        #51

                                        Daniel Pfeffer wrote:

                                        I'd then cordon off the area and decontaminate everything he touched on his visit - in an autoclave, by choice

                                        Anything wrong with fire?

                                        Wrong is evil and must be defeated. - Jeff Ello

                                        K 1 Reply Last reply
                                        0
                                        • K Kent Sharkey

                                          GitHub - samuelmarina/is-even[^]

                                          Quote:

                                          This is a 100% serious project, and it is made to help the community.

                                          A brief excerpt:

                                          function isEven(number) {
                                          if(number === 1) return false;
                                          else if(number === 2) return true;
                                          else if(number === 3) return false;
                                          else if(number === 4) return true;
                                          else if(number === 5) return false;
                                          else if(number === 6) return true;
                                          else if(number === 7) return false;

                                          They're up to 1,000,001 now. Help them grow! :doh:

                                          TTFN - Kent

                                          Sander RosselS Offline
                                          Sander RosselS Offline
                                          Sander Rossel
                                          wrote on last edited by
                                          #52

                                          Just what I was looking for! My own isEven function only goes to 1000, this is a huge step forward :D Just need to brace it properly and remove all those function exits...

                                          function isEven(number) {
                                          let result;
                                          if (number === 1) {
                                          result = false;
                                          }
                                          else if (number === 2) {
                                          result = true;
                                          }
                                          else if (number === 3) {
                                          result = false;
                                          }
                                          // ...
                                          return result;
                                          }

                                          And they have an is-odd package which is consistent with this one too. As programmers we know how important it is to be consistent. Thanks for this great find! :thumbsup: :-D

                                          Best, Sander Azure DevOps Succinctly (free eBook) Azure Serverless Succinctly (free eBook) Migrating Apps to the Cloud with Azure arrgh.js - Bringing LINQ to JavaScript

                                          1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups