• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Getting error in JavaScript

 
Ranch Hand
Posts: 367
Eclipse IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I have the below script.

I have below questions :
1. Why does it print undefined instead of 'Welcome Molly' ?
2. Should the alert statement be alert(greeting) i.e. without () ?

Thanks a lot!
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. Have you used the debugger to set breakpoints and step through the execution to answer this question.

2. What do you think? What is the difference between the two, and which should you choose give what you have assigned to greeting

3. Why are you making this unnecessarily complicated? Why are the functions nested in this way? Or is this someone else' code you are trying to figure out?
 
pramod talekar
Ranch Hand
Posts: 367
Eclipse IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Bear,

1.Yes, I did. Right till the end, the debugger shows 'return msg +getName()' as 'Welcome Molly' but just when it prints , it becomes undefined.
2.It should be greeting(), as greeting will print whatever is on right side of its =. However I didn't get how greeting() was defined.
3. It's someone else code I'm analyzing.
 
Saloon Keeper
Posts: 15489
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
greeting is a function, so greeting() evaluates to the return value of the function call. What does greeting() return?
 
pramod talekar
Ranch Hand
Posts: 367
Eclipse IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes Stephen, thanks.
I was expecting it to return 'Welcome Molly' but it returns 'undefined'.
 
Stephan van Hulst
Saloon Keeper
Posts: 15489
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, but do you know why?
 
pramod talekar
Ranch Hand
Posts: 367
Eclipse IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nope, any problem with the position of any statement?
 
Stephan van Hulst
Saloon Keeper
Posts: 15489
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nope. When does a function call have a return value?
 
pramod talekar
Ranch Hand
Posts: 367
Eclipse IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I take the getName() function out of greetuser(), then it returns 'Welcome Molly' otherwise undefined.
 
pramod talekar
Ranch Hand
Posts: 367
Eclipse IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Nope. When does a function call have a return value?


Didn't quite get this.. It returns when the return statement is executed.
 
Stephan van Hulst
Saloon Keeper
Posts: 15489
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That doesn't really answer my question. When does a function call in general have a return value?
 
pramod talekar
Ranch Hand
Posts: 367
Eclipse IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Didn't get your question..
 
Stephan van Hulst
Saloon Keeper
Posts: 15489
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

What do you need to do to make the function call evaluate to the number 3?
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Again, why have you create all the complications of nesting the functions? Without that the problem would be obvious. This is a great lesson in how making things more complicated than they need to be needlessly creates problems.
 
pramod talekar
Ranch Hand
Posts: 367
Eclipse IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:
What do you need to do to make the function call evaluate to the number 3?



It should have return 3;
 
pramod talekar
Ranch Hand
Posts: 367
Eclipse IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Bear, I'm analyzing someone else's code.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Then this is an important lesson on how not to write clear code.
 
pramod talekar
Ranch Hand
Posts: 367
Eclipse IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Bear, I have got few questions though..

1.As each function has its own execution context and as getName() has only declarative statements and not expressions, why does the alert statement displays 'undefined' instead of Welcome Molly ?
2. Even if I move the return statement down, it doesn't change the outcome.
3. If I move getName() out of greetUser(), only then it works, but then even greetUser() is a part of greeting() and it can access it's parent's variables viz. time, then what is so different about getName()?
 
Stephan van Hulst
Saloon Keeper
Posts: 15489
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

pramod talekar wrote:It should have return 3;


Correct. Now say you have a function greeting, that accepts the time of the day in hours, implement it so that it returns "Good Morning Molly" before 12 o'clock, and "Welcome Molly" after 12 o'clock.
 
pramod talekar
Ranch Hand
Posts: 367
Eclipse IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

pramod talekar wrote:1.As each function has its own execution context and as getName() has only declarative statements and not expressions, why does the alert statement displays 'undefined' instead of Welcome Molly ?



It displays the result of the greeting() function. Since the greeting() function doesn't have a return statement -- in particular it doesn't return "Welcome Molly" -- that's why you see "undefined".

2. Even if I move the return statement down, it doesn't change the outcome.
3. If I move getName() out of greetUser(), only then it works, but then even greetUser() is a part of greeting() and it can access it's parent's variables viz. time, then what is so different about getName()?



That's all irrelevant to your problem, except that if you randomly move lines of code around you might accidentally fix it. But moving lines of code around is absolutely the wrong way to think about things in any programming language.
 
pramod talekar
Ranch Hand
Posts: 367
Eclipse IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks so much Paul for your reply. But I still didn't get why Welcome Molly was not displayed.. what's the logic behind it?
If you are saying that greeting doesn't have a return statement, then why do I see 'welcome Molly' simply by moving getName() outside greetUser()?

I can't agree more with you on accidently fixing the error, that's why asking this question..
 
pramod talekar
Ranch Hand
Posts: 367
Eclipse IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another point is that, if I move getName() outside greetUser(), even then greeting() won't have any return statement, then why does 'Welcome Molly'appear instead of undefined?
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

pramod talekar wrote:Another point is that, if I move getName() outside greetUser(), even then greeting() won't have any return statement, then why does 'Welcome Molly'appear instead of undefined?



I do not see these results
 
pramod talekar
Ranch Hand
Posts: 367
Eclipse IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My bad ! Sorry for the trouble Bear and Paul.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you solved the issue?
 
pramod talekar
Ranch Hand
Posts: 367
Eclipse IDE Opera Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes Bear.
I added 'return greeting' before the end of greeting().

Thanks !
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic