0

First post; did a little digging but can't find what I'm looking for (maybe just too inexperienced with the site). Hopefully, you guys can help:

--EDIT-- Researching after discussion shows that what I was looking for was how to use return to pass a value resulting from one function, to another.

How does this relate to global/local scope? Is a value returned to a function from another local or global scope? It's local to it's original function, but accessible to global?

  • Example has been changed*

var addition = function add(a, b) { var addTotal = (a+b); return addTotal; }

 var multiply = function(c) {
 var multiplyTotal = c * 2 ; 
 return multiplyTotal; }

multiply(addition(2,3));

1

2 Answers 2

1

Make getUser return the userName, then when calling lowerUserName, pass that returned value to it as argument:

var getUser = function(userName) {
    var userName = prompt("Please enter your username?") || ''; //defend against null
    return userName;                                          // return userName
};

var lowerUserName = function(userName) {                      // expect user name as parameter (you can name this variable anything you want, it's only local to lowerUserName)
    var userNameLower = userName.toLowerCase();
                                                              // you should probably return userNameLower if you want to use it somewhere else
};

lowerUserName(getUser());                                     // call getUser and pass its return value directly to lowerUserName

lowerUserName(getUser()); can be broken into two steps to make it easy to understand:

var returnedValue = getUser();                                // the return value of getUser will be the value of userName
lowerUserName(returnValue);                                   // then we pass that value to lowerUserName when we call it
Sign up to request clarification or add additional context in comments.

8 Comments

Perfect, Ibrahim. This was exactly what I was trying to figure out. Great explanation too, and hopefully this will help others trying to understand the same thing.
@GeorgeJempty haha I know you are going to mention that null! 1. I commented because you actually said that it "returns a string", so it was just a heads-up for you. 2. OP stated that it was " just for learning purposes" so I assumed he was learning return statements which was proofed later by his comments under your answer.
@GeorgeJempty I've just learned about XY problems 5 min ago, reading the link from your comment. And I don't think XY problem is applied on a learning questions such as this, right?
Maybe not, I was thinking about posting on meta with a screenshot whether this was an X Y problem or did I really not know what I was talking about. And didn't post because I was coming to the latter conclusion LOL. Still though really trivial and not as helpful as I think the OP thinks, there are typically two ways to do these kinds of things, "chaining" (which my solution did) or function call results as arguments to other functions, as the OP's attempt and your solution did.. So if not an X Y problem, then IMO this was a case of chaining being the more succinct solution
Chaining would have worked, and I realise that. I could have quite easily done that in one function. ' var x = prompt("What is your username?").toLowerCase(); console.log(x);' WOULD have worked, but it wasn't the purpose of the question. Often we don't know what we don't know - For example, i realise my question was wrong, i was apparently asking more about returns, which i'm now going off to research more. Was the answer still useful? Heck yeah, hence, an upvote. I'll edit my OP to reflect the above though, just to be correct..
|
1

I think you might be over-complicating this, the following works, because prompt returns a string (EDIT: or null as pointed out in the first comment and I therefore trivially updated the one-liner to reflect this); see https://developer.mozilla.org/en-US/docs/Web/API/Window/prompt:

var userName = (prompt("Please enter your username?") || '').toLowerCase(); //FOOBAR
console.log(userName); //foobar

2 Comments

I probably am George. But, for now it's not so much about simplicity, rather more focused on the passing between functions themselves, than the actual operation of lowering the case of the entered username. Those particular operations were just the best related simple example i could think of.
Completely understood. I wasn't trying to learn how to use prompt or the lowercase built-in though, but more the interaction between function values as related to scope ; one local value being accessible in another function. Maybe I just don't know enough yet to know the right questions regarding to my problem, but all good. Thanks for your input anyway.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.