passing 0 to a function
-
Does anyone know what happens if you pass 0 to this function? https://pbs.twimg.com/media/GDh5G70XcAAyqTv?format=jpg&name=small Thank you.
-
Does anyone know what happens if you pass 0 to this function? https://pbs.twimg.com/media/GDh5G70XcAAyqTv?format=jpg&name=small Thank you.
Why not run it for yourself and find out? Since none of the cases match
0
, none of thereturn
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
-
Does anyone know what happens if you pass 0 to this function? https://pbs.twimg.com/media/GDh5G70XcAAyqTv?format=jpg&name=small Thank you.
-
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.
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?
-
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?
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
-
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?
-
Yes, all output is correct according to the code as written. But you can tell her to try values such as -23 and 1111.
-
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.
-
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.
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
-
Does anyone know what happens if you pass 0 to this function? https://pbs.twimg.com/media/GDh5G70XcAAyqTv?format=jpg&name=small Thank you.
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