-1

guys, I'm just starting in the coding world. Now I'm learning JavaScript, so I have a problem, I have a function that is going to ask the user for the name.

Then I want to create another function using the name for ask for an integer, so when I try to call the return values from the first function is giving me undefined.

This is my code:

function getUsername() {
    var f1CustomerName;

    f1CustomerName = prompt("Please enter your name", "Type Here");
    // window.alert("f1CustomerName is: " + f1CustomerName );

    if ( f1CustomerName == null ) {
        return;
    } else if( f1CustomerName == "Type Here" || f1CustomerName == "" ) {
        return;
    } else {
        //window.alert("Hello " + f1CustomerName + "!");
        return f1CustomerName;
    }
}

var nameUser = getUsername();

// ...

function getinteger(nameUser) {
    var integer=prompt(nameUser + " Please enter an integer");
    window.alert("Hello this is your integer: " + integer + "!");
}

getinteger();
1
  • what is enter code here, please clear your question and if possible share any online reference of you working code, somewhere on codepen.io or other online tools to share code. Commented May 30, 2020 at 6:43

4 Answers 4

2

In your version of the function you did not return anything. The following should work:

function getUsername()
  {
     var f1CustomerName = prompt("Please enter your name", "Type Here");
     if( f1CustomerName == null || f1CustomerName == "Type Here") return "";
     else return f1CustomerName;
 }
 console.log(getUsername());

Here is an even shorter version of the same. As you only have one variable in your function it makes sense to shorten its name too (to nam). You can even directly use the return value of the getUsername() function directly as a function argument in your getinteger() function call.

(But since you have nameUser in your getinteger() function's parameter list you can not access the global variable userName from within that function.)

function getUsername(){
  var nam = prompt("Please enter your name", "Type Here");
  return nam == null||nam=="Type Here" ? "" : nam ;
}
function getinteger(nameUser) {
    var integer=prompt(nameUser + " Please enter an integer");
    window.alert("Hello "+nameUser+", this is your integer: " + integer + "!");
}

getinteger(getUsername());

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, i just call my variable nameUser when i called the function getInteger
0

You should give the function the variable of nameUser to use it when calling it in the last line:

getinteger(nameUser);

Comments

0

Just return values from your if-else statements

function getUsername() {
    var f1CustomerName;

    f1CustomerName = prompt("Please enter your name", "Type Here");
    //window.alert("f1CustomerName is: " + f1CustomerName );

    if (f1CustomerName == null) {
        return f1CustomerName;
    } else if (f1CustomerName == "Type Here" || f1CustomerName == "") {
        return f1CustomerName;
    } else {
        //window.alert("Hello " + f1CustomerName + "!");
        return f1CustomerName;
    }
}

Also you should pass some argument to your getinteger function.

1 Comment

Thanks, i just call my variable nameUser when i called the function getInteger
0

You get undefined when no value is explicitly returned.

Your existing code can be drastically simplified.

function getUsername() {
  return prompt("Please enter your name") || null;
}
function getinteger(nameUser) {
  if (!nameUser) {return;}
  const integer = prompt(`${nameUser} Please enter an integer`);
  window.alert(`Hello this is your integer: ${integer}!`); 
}

getinteger(
  getUsername()
);

1 Comment

Thanks for letting me know, is cause that is just the beggining of my project i had the the loop in getusername to check any empty string or to the cancel button works, but thanks for your feedback

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.