The user gives various entries in the fields. Then when he pushes the create button I wrote a function test() to create a hero object with the users entries. But when I try to access the hero object with the button check your hero I get
mainCharacter (=name of the individualized Hero object)is not defined
What am I doing wrong? How do I get the mainCharacter Object out of the test() function?
I tried numerous return statements of the mainCharacter object within the test() function.
function Hero(name, power, endurance, lives, workedout) {
this.name = name;
this.power = power;
this.endurance = endurance;
this.lives = lives;
this.workedout = workedout;
}
// Create the hero function; takes the values from the players entries except for workedout;
function test() {
var name1 = $("#heroName").val();
var power1 = document.getElementById("heroPower").value;
var endurance1 = $("#heroEndurance").val();
var lives1 = document.getElementById("heroLives").value;
var workedout1 = false;
// CHECK IF THERE`s a STRING IN HERO NAME
switch (isNaN(name1) || parseInt(name1)) {
case true:
alert("Everything ok");
break;
default:
alert("Please enter a name");
break;
}
var mainCharacter = new Hero(name1, power1, endurance1, lives1, workedout1);
alert("His name is " + mainCharacter.name + " his power is " + mainCharacter.power + " his endurance is " + mainCharacter.endurance + " and his lives are " + mainCharacter.lives);
return mainCharacter;
};
function alertHero() {
alert(mainCharacter.lives);
}
Create your hero
Your hero`s name
Your hero`s power
Your hero`s endurance
Your hero`s lives
Run code snippetExpand snippet
JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)