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());
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.
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
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.
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.
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.