Javascript - variable scope -
i not seeing issue here. new @ javascript. have researched variable scope , looks right me. issue variable, useage, in legalage function undefined. have declared before function , passing parameter function.
"use strict"; function person(){ alert("person function 1"); var personname = prompt("enter name:", "enter name here"); var personage = parseint(prompt("enter " + personname + "'s current age:", "enter age here")); var votingstatus = legalage(); var statusmessage = (votingstatus == true) ? "old enough" : "not old enough"; document.writeln("<b>" + personname + " " + personage + " years old " + statusmessage + "</b><br />"); homecoming personage; } var useage = person(); alert("useage: " + useage); alert("outside function"); function legalage(useage) { alert("legalvoting function 2"); var canvote = (useage >= 18) ? true : false; alert("can vote: " + canvote); alert("age: " + useage); homecoming canvote; } person();
the problem haven't passed personage
legalage()
function. need:
var votingstatus = legalage(personage);
otherwise useage
in legalage()
undefined , you'll errors using it.
javascript
No comments:
Post a Comment