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

RecodeAlan
  • 17
  • 7
  • Possible duplicate of [How to use a return value in another function in Javascript?](https://stackoverflow.com/questions/19674992/how-to-use-a-return-value-in-another-function-in-javascript) – Dexygen Dec 23 '17 at 02:54

2 Answers2

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
Dexygen
  • 12,287
  • 13
  • 80
  • 147
ibrahim mahrir
  • 31,174
  • 5
  • 48
  • 73
  • 1
    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. – RecodeAlan Dec 23 '17 at 02:07
  • @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. – ibrahim mahrir Dec 23 '17 at 02:33
  • @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? – ibrahim mahrir Dec 23 '17 at 02:35
  • 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 – Dexygen Dec 23 '17 at 02:39
  • @GeorgeJempty Interesting idea about that meta post. Please do and give me the link to it so I can express my ideas there. Or should I do it? I'm really eager to find out whether learning questions fall under that XY problems or not. – ibrahim mahrir Dec 23 '17 at 02:43
  • Yeah I deleted my comment with the link to it, so here you go again https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem QUICK NOTE: That's the *old* meta site though, new one is at: https://meta.stackoverflow.com/ – Dexygen Dec 23 '17 at 02:44
  • @GeorgeJempty I've already got my bachelor's degree of this one (5min ago remember?). I was talking about the new one (this one). – ibrahim mahrir Dec 23 '17 at 02:47
  • 1
    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.. – RecodeAlan Dec 23 '17 at 02:47
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
Dexygen
  • 12,287
  • 13
  • 80
  • 147
  • 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. – RecodeAlan Dec 23 '17 at 02:01
  • 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. – RecodeAlan Dec 23 '17 at 02:24