Make a Person challenge fcc intermediate

I’m having challenges returning setFirstName(), setLastName() and setFullName()

Fill in the object constructor with the following methods below:

getFirstName()
getLastName()
getFullName()
setFirstName(first)
setLastName(last)
setFullName(firstAndLast)

Run the tests to see the expected output for each method. The methods that take an argument must accept only one argument and it has to be a string. These methods must be the only available means of interacting with the object.

var Person = function(firstAndLast) {

// Complete the method below and implement the others similarly

this.getFullName = function() {

return this.getFullName ? "Bob Ross" : "";

};

this.getFirstName = function() {

return this.getFirstName ? "Bob" : "";

};

this.getLastName = function() {

return this.getLastName ? "Ross" : "";

}

this.setFirstName = function(first) {

return this.setFirstName ? “Haskell” : “”;

}

}

var bob = new Person(‘Bob Ross’);

bob.getFullName();

bob.getFirstName();

bob.getLastName();Preformatted text

why are you returning the method itself in all cases?
that’s not what they should do

try thinking first what each method should do, and try to make sure that it does that

Ok let me think about that

what could I be doing wrong

var Person = function(firstAndLast) {

// Complete the method below and implement the others similarly

this.setFirstName = function(first) {

  return first;

}

this.setLastName = function(last) {

  return last;

}

this.setFullName = function(firstAndLast) {

  return firstAndLast;

}

this.getFullName = function() {

if(this.setFullName) {

return this.setFullName;

}

else if(this.setFirstName) {

return this.setFirstName + this.getLastName;

}

else if(this.setLastName) {

return this.getFirstName + this.setLastName;

}

}

this.getFirstName = function(first) {

 return this.getFirstName ? first : this.setFirstName;

}

this.getLastName = function(last) {

return this.getLastName ? last : this.setLastName;

}

}

var bob = new Person(‘Bob Ross’);

bob.getFullName();

bob.getFirstName(“Bob”);

bob.getLastName(“Ross”);

bob.setFirstName(“Haskell”);

bob.setLastName(“Curry”);

bob.setFullName(“Haskell Curry”);

you are not changing anything, just returning values

you need a variable to store the name inside Person and change the value of this variable using the various set methods and return values from this variable with the get methods

How do I store the name inside person

variables can be used for holding values - what kind of value? depends on the logic you choose to use, it can be an array, an object, a string…

I meant how do I access the first name and last name. let’s say am using an array so how do i access them. Can I access as arguments or how do I split them and obtain them in parts

you need to store the values somewhere

you can’t just use the argument, you need to have a local variable

then you access the values from this variable as from any other variable

Consider going back and redoing the object oriented programming section. The goal of the set methods is to change the value of variables that live inside the object, and the goal of the get methods is to return the value of those variables.

I.e the set methods don’t need to return anything and should go something like this…

this.setSomeVariable = (value) => {
  myVariable = value;
}

and the get methods should simply return the value of the desired variable. for example

this.getSomeVariable = () => {
  return someVariable;
}

and if you’re trying to split the individual names from the firstAndLast string first split it then you can access them individually

str = str.split(' ')
let firstWord = str[0]
let secondWord = str[1]

Object JS - Challange

but I did and it works :smiley: