/    Sign up×
Community /Pin to ProfileBookmark

constructor function blues..

Hey everyone, can anyone tell me why this constructor function isn’t working out. (I’m getting an undefined for avgGrade). As far as I understand the variable name that holds the value of the property that i’m defining in the contructor function can be anyname, yet this script doesn’t work…
As always, thanks in advance!

var yourName = window.prompt(“What’s your name?” , “”);
var yourAge = window.prompt(“What’s your age?” , “”);
var yourGrade = window.prompt(“What’s your G.P.A?” , “”);
var yourSex = window.prompt(“What’s your sex?” , “”);
function student(name,age,avgGrade,sex){
this.name = name;
this.age = age;
this.grade = avgGrade;
this.sex = sex;
}

var student1 = new student(yourName, yourAge, yourGrade, yourSex)

document.write(student1.avgGrade)

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@Khalid_AliAug 16.2005 — to me logic seems incorrect..what u need to do is get all of the user info in a function that is called on the page "onload" event. then pass the params to the constructor and use the values afterwards
Copy linkTweet thisAlerts:
@NoAssmblyReqdauthorAug 16.2005 — Thanks Khalid, yea its not a meaningful script. It was just something I was playing around with as i'm learning. I realized why I got an undefined though. I was referencing the variable and not the property itself.

Thanks for the reply though.
×

Success!

Help @NoAssmblyReqd spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...