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

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);

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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.