Win a copy of TensorFlow 2.0 in Action this week in the Artificial Intelligence and Machine Learning forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Getting error in JavaScript

 
Ranch Hand
Posts: 367
Eclipse IDE Opera Java
  • Mark post as helpful
  • send pies
  • 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!
 
Marshal
Posts: 67463
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • 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
  • 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: 12430
269
  • Mark post as helpful
  • send pies
  • 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
  • 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: 12430
269
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Nope, any problem with the position of any statement?
 
Stephan van Hulst
Saloon Keeper
Posts: 12430
269
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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: 12430
269
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Didn't get your question..
 
Stephan van Hulst
Saloon Keeper
Posts: 12430
269
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

What do you need to do to make the function call evaluate to the number 3?
 
Bear Bibeault
Marshal
Posts: 67463
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • Quote
  • Report post to moderator
Hi Bear, I'm analyzing someone else's code.
 
Bear Bibeault
Marshal
Posts: 67463
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • 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
  • 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: 12430
269
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator

 
Marshal
Posts: 25960
70
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
Marshal
Posts: 67463
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
My bad ! Sorry for the trouble Bear and Paul.
 
Bear Bibeault
Marshal
Posts: 67463
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Yes Bear.
I added 'return greeting' before the end of greeting().

Thanks !
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic