1,105,395 Community Members

newb Js question about function return global variable reasssignment

Member Avatar
Proglearner
Light Poster
43 posts since Jan 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Have a look at this snippet:

var changeNumber = function(number)    {
    return number + 5;
};

var newNumber = 4;
changeNumber(newNumber);
alert(newNumber);

I've been taught that the function should reassign whatever value it returns to the global variable NewNumber, so that newNumber should change from 4 to 9. This doesn't, however, seem to happen when I test-run the code; newNumber retains its original value. So where'd I go wrong?

Prog

Member Avatar
stbuchok
Posting Shark
957 posts since May 2011
Reputation Points: 123 [?]
Q&As Helped to Solve: 142 [?]
Skill Endorsements: 2 [?]
 
0
 
Member Avatar
gon1387
Posting Whiz in Training
242 posts since Jan 2011
Reputation Points: 22 [?]
Q&As Helped to Solve: 38 [?]
Skill Endorsements: 4 [?]
 
0
 

Hi proglearner,
You forget to catch the value. :)

Your code should have been done like this:

var changeNumber = function(number){
    return number + 5;
};
var newNumber = 4;
// Should catch the returned value from changeNumber function
newNumber = changeNumber(newNumber);
alert(newNumber);
Member Avatar
Proglearner
Light Poster
43 posts since Jan 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Geez, don't I feel like a total dumbfrigg now. :D Thank you!

Member Avatar
gon1387
Posting Whiz in Training
242 posts since Jan 2011
Reputation Points: 22 [?]
Q&As Helped to Solve: 38 [?]
Skill Endorsements: 4 [?]
 
0
 

Please make this thread solved, if everything's ok now.

Question Answered as of 1 Year Ago by gon1387 and stbuchok
Member Avatar
Proglearner
Light Poster
43 posts since Jan 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Marked. :)

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article