0

When I console.log, this code:

 var Student = function(name, address, gpa){

    console.log(this);
    this.name = name;
    this.address = address;
    this.gpa = gpa;

    console.log("name is equal to " + name);
    console.log("address is equal to " + address);
    console.log("gpa is equal to " + gpa);
};



    var studentCall = [
    new Student ({
        name: "Marshae Hannor",
        address:{
            street: "345 Main St",
            city: "Smyrna",
            state: "GA"},
        gpa: [2.5, 3.5, 4.0]}),
    new Student ({
        name: "Vernon Owens",
        address:{
            street: "439 Serious St",
            city: "Baltimore",
            state: "MD"},
        gpa: [3.5, 3.2, 3.7]})
];

this is what I get in the console.log

Object {}
main2.js (line 39)
name is equal to [object Object]
main2.js (line 44)
address is equal to undefined
main2.js (line 45)
gpa is equal to undefined
main2.js (line 46)
Object {}
main2.js (line 39)
name is equal to [object Object]
main2.js (line 44)
address is equal to undefined
main2.js (line 45)
gpa is equal to undefined

Can someone help me understand what Im doing incorrectly. Thanks

1 Answer 1

2

In your calls to Student, you're passing in a single argument, which is an object that looks like this:

{
    name: "Marshae Hannor",
    address:{
        street: "345 Main St",
        city: "Smyrna",
        state: "GA"},
    gpa: [2.5, 3.5, 4.0]
}

But your Student function expects to receive three discrete arguments. So you'd call it without the { and } and without giving the names of the arguments:

var studentCall = [
    new Student (
        "Marshae Hannor",
        {
            street: "345 Main St",
            city: "Smyrna",
            state: "GA"},
        [2.5, 3.5, 4.0]),
    new Student (
        "Vernon Owens",
        {
            street: "439 Serious St",
            city: "Baltimore",
            state: "MD"},
        [3.5, 3.2, 3.7])
];

Or, modify Student to expect to receive just a single object with those properties:

var Student = function(obj){

    console.log(this);
    this.name = obj.name;
    this.address = obj.address;
    this.gpa = obj.gpa;

    console.log("name is equal to " + this.name);
    console.log("address is equal to " + this.address);
    console.log("gpa is equal to " + this.gpa);
};
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks TJ Crowder, your second with obj, worked. Thanks a million! I wholeheartedly appreciate it! Can't thank you enough right now and I understand it, sorry but I've only been coding for a month in a half. Thanks so much
@user2956271: No apology required! I'm glad this helped. Keep at it, you clearly have the knack.
Wow that is such a compliment, especially coming from someone who have been doing this for the amount of time you have. I will keep at it, I know I want to do web developing for life. Thanks for the compliment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.