Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why when I get undefined when I console.log(createdAnimal)?

What I am trying to achieve in this code is to be able to console.log(createdAnimal) and then get the code to print out the objectAnimal with these parameters:

animalMaker('cat','flying',true);

It works when I invoke the animalMaker function, but I need it to work when I console.log(createdAnimal).

Thank you in advance!

Here is the code:

function animalMaker(inputType, inputSuperPower, inputCanFly){
  var objectAnimal = {
    'type': inputType,
    'inputSuperPower': inputSuperPower,
    'inputCanFly': inputCanFly,
    'createdBy': 'Scotty'
  };
  console.log(objectAnimal)
}

var createdAnimal = animalMaker('cat','flying',true); 

console.log(createdAnimal);
like image 421
user3522720 Avatar asked Nov 22 '25 08:11

user3522720


1 Answers

You need to return the object from the function:

function animalMaker(inputType, inputSuperPower, inputCanFly){
  var objectAnimal = {
    'type': inputType,
    'inputSuperPower': inputSuperPower,
    'inputCanFly': inputCanFly,
    'createdBy': 'Scotty'
  };

  return objectAnimal;
}
like image 134
Charlie Avatar answered Nov 23 '25 22:11

Charlie



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!