in Education by
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)

1 Answer

0 votes
by
You should take a look at scope. A quick fix might be something like this: var mainCharacter; ... function test() { // same as your old code mainCharacter = new Hero(...); } function alertHero() { alert(mainCharacter.lives); }

Related questions

0 votes
    The user gives various entries in the fields. Then when he pushes the create button I wrote a function ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 21, 2022 in Education by JackTerrance
0 votes
    is there a way to validate the pattern of an input by it's event object (without using a form)? ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 5, 2022 in Education by JackTerrance
0 votes
    is there a way to validate the pattern of an input by it's event object (without using a form)? ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 3, 2022 in Education by JackTerrance
0 votes
    Course Results (${courseResponseList.getCourses().size()}) Want to show above div. jquery script. jQuery. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 6, 2022 in Education by JackTerrance
0 votes
    Course Results (${courseResponseList.getCourses().size()}) Want to show above div. jquery script. jQuery. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 20, 2022 in Education by JackTerrance
0 votes
    This question already has an answer here: Object.assign getters and setters in constructor (1 answer) Closed ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 17, 2022 in Education by JackTerrance
0 votes
    This question already has an answer here: Object.assign getters and setters in constructor (1 answer) Closed ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 17, 2022 in Education by JackTerrance
0 votes
    I created a raycaster that allows me to pick an object from the scene with my mouse like such: ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 9, 2022 in Education by JackTerrance
0 votes
    hello I have the following code that maps through my text and prints typography, {['Example1:', 'Example2 ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 2, 2022 in Education by JackTerrance
0 votes
    hello I have the following code that maps through my text and prints typography, {['Example1:', 'Example2 ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 22, 2022 in Education by JackTerrance
0 votes
    Is there a better way than this to splice an array into another array in javascript var string = ' ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 10, 2022 in Education by JackTerrance
0 votes
    There are loads of questions about the current date in JavaScript in general, but in my scenario, I have ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 3, 2022 in Education by JackTerrance
0 votes
    i have a website that contain an iframe. www.vendeposto.com.br and i want to hide an element Im trying to do this ... the vendeposto.com.br site is this: Postos de gasolina a venda...
asked May 1, 2022 in Education by JackTerrance
0 votes
    i have a website that contain an iframe. www.vendeposto.com.br and i want to hide an element Im trying to do this ... the vendeposto.com.br site is this: Postos de gasolina a venda...
asked Apr 29, 2022 in Education by JackTerrance
0 votes
    i have a website that contain an iframe. www.vendeposto.com.br and i want to hide an element Im trying to do this ... the vendeposto.com.br site is this: Postos de gasolina a venda...
asked Apr 26, 2022 in Education by JackTerrance
...