Intermediate Algorithm Scripting: Make a Person

Tell us what’s happening:
Can someone please explain me, Why I don’t meet the criteria of six keys and why I do not get two tests failed.

Thanks in Advance for valuable feedback and solutions.

Your code so far


var Person = function (firstAndLast) {
  // Complete the method below and implement the others similarly
  var firstName = firstAndLast.split(" ")[0];
  var lastName = firstAndLast.split(" ")[1];

  this.setFirstName = function (firstName) {
    this.firstName = firstName;
    return this.firstName;
  }

  this.setLastName = function (lastName) {
    this.lastName = lastName;
    return this.lastName;
  }

  this.setFullName = function (fullName) {
    firstName = fullName.split(" ")[0];
    lastName = fullName.split(" ")[1];
    this.fullName = firstName + " " + lastName;
    return this.fullName;
  }

  this.getFullName = function () {
    this.fullName = this.firstName + ' ' + this.lastName
    console.log(this.fullName);
    return this.fullName;
  };

  this.getFirstName = function () {
    this.firstName = firstName;
    console.log(this.firstName);
    return this.firstName;
  };

  this.getLastName = function () {
    this.lastName = lastName;
    console.log(this.lastName);
    return this.lastName;
  };
};

var bob = new Person('Bob Ross');
bob.getFullName();

Link to the challenge:

You’re using this.firstName and this.lastName. Those properties count as another 2, and it’s supposed to get stored in the firstName and lastName properties you already declared. With that, all the tests should pass.

Those two lines should be used instead of this.firstName = whatever, etc.

Hope this helps! :smiley:

1 Like

Hi, Steffan

Thanks for kind reply, However I am not getting what you mean.

var firstName = firstAndLast.split(" ")[0]; 
var lastName = firstAndLast.split(" ")[1];

These the are local variables, Why those two count as keys. Please explain

Those two don’t count as keys, but when you start using this.lastName, this.firstName and this.fullName those count as keys. Don’t make them properties of the object, just local variables, that you set or recover using the methods.

2 Likes

Thanks man, I got you. Challenge is done with your suggestions. I had this weird thought we have specify properties and this.Something, If we need to retrieve them inside the idea. I have completely took that wrong. Need to work on this key work and scope chain again.

Many thanks man. Cheers…

Please don’t post full solutions here, otherwise people can just find the answer when they didn’t even want one, etc.

Hi Steffan

Thanks, Took the answer out.

I know this is a little bit of an old thread, but I’m a little confused about your reply @ilenia!

If those variables are declared within the scope of the constructor function, but not of the inheriting instance, will they not be changed for all instances if we supply a new name for bob?

I might be missing something…

if you create a new instance of Person, those will be created anew for this instance of person. Like, if you do let john = new Person("John Smith")
the variables will belong to John

the thing that differ between having the name as key or as local variable is if they can be externally changed: if you set them as keys you can do john.name = "Bobby"
but instead we want to have the names changed only using the methods we have to create for this challenge, so the name can’t be a property too.

Ah! Thanks @ilenia, the fact that the instance methods are closures, with their own instances of the constructor’s variables, was the missing piece for me. I’m still a little hazy on how closures work but will read up about it.

I’ve tried to document the process here in case it helps others:

Can you see any issues with my visualisation?

Most of it I am knowledged enough on the exact mechanism to evaluate, but the sort where it says that the methods capture a copy of the variables doesn’t convince me.

The methods are defined inside the object, so they have access to what else is also defined inside the object, they do not copy the variables, they are able to access them
a copy happens only if explicitly written

1 Like

Yeah, I agree with your comments and I’m not convinced that I’ve fully grasped that process either. I plan to read up on it a bit more and update that visualisation accordingly.

Where I get a little confused is why updating the variable in the new instance (via the setter method) doesn’t update the variable for all other instances if it’s not a copy. But don’t worry—I’ll go digging for the answer to that. For now, within the scope of this discussion, let’s consider the case clos…ured.

simplifying a bit, the new keyword creates a copy of the Person object. if you make changes to bob it will not have effect on john as both have been originally created as a copy of Person and are not linked to each other

Thanks, and am I right in saying that the “function part” of the object isn’t copied? Ie. I won’t be able to go new bob() and expect the constructor to run?

bob is an object, not a function, so that shouldn’t work. You can always try and see what happens. Probably you get an error of some kind.

Yep, I did try that. I have to get my head around the fact that the object is copied with new but the function isn’t for some reason. I’ll get there!

you have

var Person = function (firstAndLast) {...}

this function is a constructor, and if you use new on Person you create a new object using this constructor function, this new object will have the properties defined from the constructor function:

a resource on this
https://www.w3schools.com/js/js_object_constructors.asp

1 Like

Thank you to whoever restarted this thread, because this particular problem seems to have utterly defeated me.
I thought that I understood the idea of properties and constructor functions inside an object and the issues around scope, but that is clearly not the case.
Any pointers as to where I am going wrong would be much appreciated. I have looked at the previously suggested references, including the w3schools site, and have to admit to still being puzzled by this.

var Person = function(firstAndLast) {
  // Complete the method below and implement the others similarly
  var a = firstAndLast.split(" ");
  var firstName = a[0];
  var lastName = a[1];
  var fullName = firstAndLast;

  this.setFirstName = function(firstName) {
    this.firstName = firstName;
    return this.firstName;
  };
  this.setLastName = function(lastName) {
    this.lastName = lastName;
    return this.lastName;
  };
  this.setFullName = function(fullName) {
    var f = fullName.split(" ");
    firstName = f[0];
    lastName = f[1];
    this.fullName = firstName + " " + lastName;
    return this.fullName;
  };

  this.getFirstName = function() {
    this.firstName = firstName;
    return this.firstName;
  };
  this.getLastName = function() {
    this.lastName = lastName;
    return this.lastName;
  };
  this.getFullName = function() {
    fullName = this.firstName + ' ' + this.lastName;
    return this.fullName;
  };
};

var bob = new Person('Bob Ross');
bob.getFullName();

you have this and then you start using this.firstName&Co.

why?

Thanks for your response. I am obviously a little confused. If I don’t use the this keyword, I thought that the object’s properties would not be changed outside of the function. I have changed the code but can’t get the new fullName to work when the first and last name is changed. Thanks again for your help.

var Person = function(firstAndLast) {
  // Complete the method below and implement the others similarly
  var a = firstAndLast.split(" ");
  var firstName = a[0];
  var lastName = a[1];

  this.setFirstName = function(firstName) {
    firstName = firstName;
    return firstName;
  };
  this.setLastName = function(lastName) {
    lastName = lastName;
    return lastName;
  };
  this.setFullName = function(fullName) {
    var f = fullName.split(" ");
    firstName = f[0];
    lastName = f[1];
    fullName = firstName + " " + lastName;
    return fullName;
  };

  this.getFirstName = function() {
    return firstName;
  };
  this.getLastName = function() {
    return lastName;
  };
  this.getFullName = function() {
    return firstName + " " + lastName;
  };
};

var bob = new Person('Bob Ross');
bob.getFullName();