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. Web Development
  3. JavaScript
  4. passing 0 to a function

passing 0 to a function

Scheduled Pinned Locked Moved JavaScript
comquestion
10 Posts 5 Posters 20 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.
  • M Offline
    M Offline
    mike7411
    wrote on last edited by
    #1

    Does anyone know what happens if you pass 0 to this function? https://pbs.twimg.com/media/GDh5G70XcAAyqTv?format=jpg&name=small Thank you.

    R L J 3 Replies Last reply
    0
    • M mike7411

      Does anyone know what happens if you pass 0 to this function? https://pbs.twimg.com/media/GDh5G70XcAAyqTv?format=jpg&name=small Thank you.

      R Offline
      R Offline
      Richard Deeming
      wrote on last edited by
      #2

      Why not run it for yourself and find out? Since none of the cases match 0, none of the return statements will execute. The return value of the function will be undefined.


      "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
      • M mike7411

        Does anyone know what happens if you pass 0 to this function? https://pbs.twimg.com/media/GDh5G70XcAAyqTv?format=jpg&name=small Thank you.

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        C++, Java and now Javscript. Maybe you should focus on one language and learn it completely before moving to the next one. Especially as the code in the above question would behave exactly the same (including doing nothing for zero) in all three.

        M 1 Reply Last reply
        0
        • L Lost User

          C++, Java and now Javscript. Maybe you should focus on one language and learn it completely before moving to the next one. Especially as the code in the above question would behave exactly the same (including doing nothing for zero) in all three.

          M Offline
          M Offline
          mike7411
          wrote on last edited by
          #4

          I am trying to help a lass with her JavaScript code. Take a look at this image she posted: https://pbs.twimg.com/media/GDpFl1SWcAA0jk1?format=jpg&name=small[^] She claims the blue lines next to console.log are the output. Does it make sense to you?

          D L 2 Replies Last reply
          0
          • M mike7411

            I am trying to help a lass with her JavaScript code. Take a look at this image she posted: https://pbs.twimg.com/media/GDpFl1SWcAA0jk1?format=jpg&name=small[^] She claims the blue lines next to console.log are the output. Does it make sense to you?

            D Offline
            D Offline
            Dave Kreskowiak
            wrote on last edited by
            #5

            That code does not output what you/she says it does. The first console.log statement will output 'undefined'. The second will output 'One digit: ${x}'. The third will output 'Two digit: ${x}'. and the last will output 'The number is: ${x}'. The 'undefined' is output because none of the cases in the switch block met any criteria so it fell through to the last (and non-existent) statement in the numberDigits function. There is no return statement there either, so the function ends without returning anything. Javascript will set the 'return value' to undefined, or null in other languages. Other languages will just crash or not even compile, but since javascript is interpreted, it can get away with an undefined return value.

            Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles. Dave Kreskowiak

            1 Reply Last reply
            0
            • M mike7411

              I am trying to help a lass with her JavaScript code. Take a look at this image she posted: https://pbs.twimg.com/media/GDpFl1SWcAA0jk1?format=jpg&name=small[^] She claims the blue lines next to console.log are the output. Does it make sense to you?

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              Yes, all output is correct according to the code as written. But you can tell her to try values such as -23 and 1111.

              M 1 Reply Last reply
              0
              • L Lost User

                Yes, all output is correct according to the code as written. But you can tell her to try values such as -23 and 1111.

                M Offline
                M Offline
                mike7411
                wrote on last edited by
                #7

                Are you saying that passing 0 doesn't return undefined? Thanks.

                L 1 Reply Last reply
                0
                • M mike7411

                  Are you saying that passing 0 doesn't return undefined? Thanks.

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  Clearly not as the test results show. Look at the last case statement:

                  case x > 0 || x <= 100:

                  That means if x is greater than zero OR if x is less than or equal to 100! So any postive number (which is greater than zero) OR any number less than 101 (which includes negative numbers). I suspect that is not what the code is supposed to do.

                  D 1 Reply Last reply
                  0
                  • L Lost User

                    Clearly not as the test results show. Look at the last case statement:

                    case x > 0 || x <= 100:

                    That means if x is greater than zero OR if x is less than or equal to 100! So any postive number (which is greater than zero) OR any number less than 101 (which includes negative numbers). I suspect that is not what the code is supposed to do.

                    D Offline
                    D Offline
                    Dave Kreskowiak
                    wrote on last edited by
                    #9

                    I screwed that up in my test code. You are correct.

                    Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles. Dave Kreskowiak

                    1 Reply Last reply
                    0
                    • M mike7411

                      Does anyone know what happens if you pass 0 to this function? https://pbs.twimg.com/media/GDh5G70XcAAyqTv?format=jpg&name=small Thank you.

                      J Offline
                      J Offline
                      Jeremy Falcon
                      wrote on last edited by
                      #10

                      You'd be better off putting just a little effort into your question. Not everyone wants to click an external link for an image of code for instance. People are usually happy to help as long as they aren't being used. That being said, as mentioned, you and the person you're helping should learn to run code and test it yourself. If that's not something you're willing to do, then neither of you two need to be programming. This isn't being mean man; this is saying the industry doesn't need another coder who won't even run own code they're testing. At least run it with passing in zero and then asking _why_ a return value is what it is.

                      Jeremy Falcon

                      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