How to separate names correctly?

I think maybe I have a good start. But with something like var bob = new Person('Bob Ross'); and then bob.setFirstName("Haskell") the setFirstName only updates first and not full. So I am thinking I need to call the setFullName method inside of both setFirstName & setLastName. Except the parameter passed to setFullName is a single string and I’m unsure how to separate them without incrementing .keys().length.

  var Person = function(firstAndLast) {
  // Only change code below this line
  // Complete the method below and implement the others similarly
  this.setFirstName = function(first) {
    this.first = first;
  }
  this.setLastName = function(last) {
    this.last = last;
  }
  this.setFullName = function(firstAndLast) {
    // console.log(firstAndLast.split(" ")[1]); 
    this.first = firstAndLast.split(" ")[0];
    this.last = firstAndLast.split(" ")[1];
    // this.full = firstAndLast;
  }
  this.getFirstName = function() {
    return this.first;  
  }
  this.getLastName = function() {
    return this.last; 
  } 
  this.getFullName = function() {
    return firstAndLast;
  };
  return firstAndLast; 
};

var bob = new Person('Bob Ross');
console.log(bob instanceof Person);
// console.log(bob.getFirstName());
// console.log(bob.getLastName());
console.log(bob.setFullName("Jim Jones"));
console.log(Object.keys(bob).length)
bob.setFirstName("Haskell"); 
console.log(bob.getFullName());

Two thoughts for you

  1. If you have the first and the last, can you rebuild the full name?

  2. You can declare variables inside of tme context of an object without binding them to this.

It sets the first name just like the function says, not the full name. Why are you expecting something different when you have functions to set just the last name or the full name?

I don’t think this is doing what you think it does.

Look at the hint to see how you can fix this. I normally wouldn’t say look at the hint but I think you can learn from it.

Yes but I don’t have the first and last. There is only one full name parameter passed in when declaring a new person object.

But you know how to take apart a single name…

I know but if we have already set the full name then if we call setFirstName we need to change the full name too i.e. we need to change the full name as well and I was asking how.

It is creating a new Person object, passing in ‘Bob Ross’ to the constructor, and immediately returning the string without saving the string to any variable inside the object.
Except that

this.setFullName = function(firstAndLast) {
    this.full = firstAndLast; 
  }
this.getFullName = function() {
    return this.full;
  };

returns undefined in getter. I already looked at hint and didn’t find it to be that helpful.

    let full = firstAndLast; 

does not work either.

Make your life easier here. Pick one

  1. store and update the full name only
  2. store and update the first and last names only

Do not mix and match.

1 Like

#2 - So it would look something like this?

var Person = function(firstAndLast) {
  // Only change code below this line
  // Complete the method below and implement the others similarly
  this.setFirstName = function(firstAndLast) {
    this.first = firstAndLast.split(" ")[0];
  }

No. You don’t get to arbitrarily change how you interpret the input of the setters. The meaning of those variables is fixed. You have to respect the function signatures that were provided.

You control how you store the data you get from the setters.

And again, don’t save the first or last names to a variable bound to this.

But that’s what I’m saying though. If I don’t get to change how I interpret the input, then how I am I supposed to break them up into first and last if one full name string is passed?

So like this?

this.setFirstName = function(first) {
    let first = first;
  }

Does not set the full name, first or last, in your class Person. Those are methods to call on instances of the Person class, i.e. bob. The methods are not called when you create a new instance, which in you’re code the instance is “bob”. One of the things your class is lacking is setting the first name, last name, or full name when an instance is declared.

Let’s back way up.

In this challenge we’re defining the constructor for an object.

Your constructor must do four things

  1. take in and set a full name
  2. define setters to update the first, last, and full names
  3. define getters to return the first, last, and full names
  4. return the full name as a string

Now, item 4) is already done for you. Items 2) and 3) are explicit in the instructions. Item 1) is up to you to decide how you want to do.

Item 1) is where this advice comes into play:

Redefining variables inside of the setters with let will not help you because of scope.

My understanding was that

var Person = function(firstAndLast) {

was the start of the constructor function and everything inbetween it and ending curly bracket was called upon the creation of a new Person object as long as the method had
firstAndLast sent in as a parameter.

A function definition and a function call are different things. When you define a function, you aren’t calling it, unless you are writing an immediately invoked function, which you aren’t here. You’re only defining the getters and setters.

Right so if at the bottom I write

var jim = new Person(); 
console.log(jim.setFullName("Jim Jones"));
console.log(jim.getFullName());

and in object I have:

this.setFullName = function(firstAndLast) {
  this.full = firstAndLast; 
  }
this.getFullName = function() {
    return this.full;
  };

looks like it returns what I need even though you said don’t use this.

Don’t do this.full

It breaks this requirement

These methods must be the only available means of interacting with the object.

Because then your getters and setters can be bypassed.


Also, you should provide a name here

when you create a new Person.

Well then I am stumped bc

  let full = firstAndLast; 
 return full; 

returns undefined.

I need more code to be able to comment. What’s your full constructor now?

var Person = function(firstAndLast) {
  // Only change code below this line
  // Complete the method below and implement the others similarly
  this.setFirstName = function(first) {
    //this.first = first;
  }
  this.setLastName = function(last) {
    //this.last = last;
  }
  this.setFullName = function(firstAndLast) {
    let full = firstAndLast; 
  }
  this.getFirstName = function() {
    //return this.first;  
  }
  this.getLastName = function() {
   // return this.last; 
  } 
  this.getFullName = function() {
    return full;
  };
  return firstAndLast; 
};

I don’t think anyone said not to do that, your code works because that is what you coded, the full name or first and last name should be set when you create a new Person.

Your goal should be to create a new person and pass in the person’s full name.

What you are doing here:

Is calling the setFullName method on the jim instance, which sets the full name. That is not what you want - to declare a new instance and the have to populate the full name by calling a method after creating an instance.