grizzly23
grizzly23

Reputation: 1

Why the return value of my function is undefined?

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

Upvotes: 0

Views: 67

Answers (4)

Carsten Massmann
Carsten Massmann

Reputation: 28196

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

Upvotes: 2

Chase
Chase

Reputation: 3126

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

Upvotes: 0

Aman Kumayu
Aman Kumayu

Reputation: 389

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.

Upvotes: 0

Alae Essaki
Alae Essaki

Reputation: 78

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

getinteger(nameUser);

Upvotes: 0

Related Questions