2

I am newbie to Javascript and the syntax just bugs me. The output is giving me undefined as an alert after every function declaration output.

let func=[
    {
        sayHi:function(){
            alert('hello');
        },
        saybi:function(){
            alert('Bye');
        }
    },
    {
        sayName:function(name){
            alert('Hii !! ', +name);
        },
        askName:function(){
            alert("What's your name ??");
        }
    }
];

alert(func[0].sayHi());

let name=prompt("What's your name ?","");
if(name!="")
{
    alert(func[1].sayName(name));
}
else{
    alert(func[1].askName());
}
2
  • Actually if u notice i just gave a comma between so func[1] is defined. The thing is that the output does come but after that undefined comes consequently Commented May 1, 2019 at 22:32
  • Formatting code is important for readability, :) Commented May 1, 2019 at 22:33

3 Answers 3

2

functions in func array call alert function. But you have called alert again after invoking a function like alert(func[0].sayHi()).

You should call function like below:

let func = [{
    sayHi: function() {
      alert('hello');
    },
    saybi: function() {
      alert('Bye');
    }
  },
  {
    sayName: function(name) {
      alert('Hii !! ' + name);
    },
    askName: function() {
      alert("What's your name ??");
    }
  }
];

func[0].sayHi();

let name = prompt("What's your name ?", "");
if (name != "") {
  func[1].sayName(name);
} else {
  func[1].askName();
}

Update: I've fixed sayName function to work properly.

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

2 Comments

Or just return the strings (either 'hello' or 'bye') from those functions with an alert call below. But your answer is clearer I guess. The overall point is that those functions returned nothing, hence the undefined after calling alert with those functions as parameters.
Thanks !! It helped. Keep helping us newbies. :p
2

You don't need to wrap every function call with alert() as those functions returns undefined and confuses you. Also alert() takes one argument, so the comma (',') there isn't helping.

Try this:

let func = [
    {
      sayHi: function(){
        alert('hello');
      },
      saybi: function(){
        alert('Bye');
      },
    },{
      sayName: function(name) {
        alert('Hi !! ' + name);
      },
      askName: function() {
        alert("What's your name ??");
      },
    }];

func[0].sayHi();

let name = prompt("What's your name ?", "");
if (name != "") {
    func[1].sayName(name);
} else {
    func[1].askName();
}

1 Comment

Great !! Thanks for Help.
0

It works perfect. sayHi() returns nothing so it returns undefined. That is it what you are alerting in the last line.

Comments

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.