Need a better understanding of make a person

`Tell us what’s happening:

explanation just below written with the code

Your code so far


var Person = function(firstAndLast) {
// Complete the method below and implement the others similarly


//I have tried to simplify this problem down so I can get a better understanding of it.

//I wanted to return the result of getFullName by accessing the returned value from setFirstName and setLastName and concatenating them together. One particular point of confusion is in setFirstName and setLastName if I uncomment the var firstName and var lastName in each method(see methods below)  the getFullName seems to be able to access the returned values and print the output to the console. However when I try to access the the paramenter first and last getFullName (the uncommented code) getFullName can't access the parameters.

//I think I have some confusion around what exactly is meant when talking about getting and setting. From my perspective, the setFirstName, setLastName means the method will take an argument and return it . Then the getFirstName and getLastName should somehow access the result from setFirstName or setLastName and return the value. Is this correct?

this.getFullName = function() {
  var getFirstName = this.setFirstName();
  var getlastName = this.setLastName();
  
  console.log( getFirstName + " " + getlastName)
};



this.setFirstName = function(first) {
var firstName = first;
//var firstName = "first"
return firstName;
}


this.setLastName = function(last) {
var lastName = last;
//var lastName = "last"
return lastName;

};



};

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


Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36.

Challenge: Make a Person

Link to the challenge:
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/make-a-person

your Person has a parameter, firstAndLast, this is where the starting name comes from, and you need to find a way to keep it, return it as requested, and change it as requested and keep on memory the updated name

you never use the parameter firstAndLast

ahh right thanks for your response, so previously I had tried using split() on firstandlast to access the firstname and lastname, maybe I will try that again combined with some of the elements from this approach. something like this
using split() to access the first and last names

this.getFullName = function() {
    var getFirstName = this.setFirstName();
    var getlastName = this.setLastName();
    
    console.log( getFirstName + " " + getlastName)
  };


  
this.setFirstName = function(first) {

var firstName = firstAndLast.split(" ")[0]
//var firstName = "first"
return firstName;
}


this.setLastName = function(last) {
var lastName = firstAndLast.split(" ")[1]
//var lastName = "last"
return lastName;

  };

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.

markdown_Forums

you need to consider how to keep in memory the new name after one of the set methods is called. Right now the change exist only inside the method, but the new name needs to be accessed by the get methods

also, you can’t create one of the required methods by renaming a different method, they need to do different things

Thanks for the help and format improvement, I will continue working on the problem!

Not quite sure about where to find out how to progress here beyond what I have below. I have been reading about bind() and passing values by reference and value .Is there some documentation that explains how the setter should work.?

  // Complete the method below and implement the others similarly


  this.getFullName = function() {
    var getFirstName = this.setFirstName();
    var getlastName = this.setLastName();
    return getFirstName + " " + getlastName
  };



this.getFirstName = function() {
var getFirstName = this.setFirstName()
return getFirstName

}

this.getLastName = function() {
var getLastName = this.setLastName()
console.log(getLastName)
return getLastName

}
  
this.setFirstName = function(first) {
var firstName = firstAndLast.split(" ")[0]
return firstName;
}


this.setLastName = function(last) {
var lastName = firstAndLast.split(" ")[1]
return lastName;

  };



};

var bob = new Person('Bob Ross');
bob.getFullName();
bob.setFirstName();
bob.setLastName();
bob.getFirstName();
bob.getLastName();

You can’t create a method by using an other of the methods you have created, it can’t work like that, they need to do different things. Also the set methods always need an argument.

you are still missing the most important part, which is saving inside the instance of Person its name, and then updating it as needed. But not as a property.
I will give you an hint with a link to a previous lesson, which you can use as an example on how to also the get methods work. Once you can return reliably things, then the set methods are much easier to create.

https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/object-oriented-programming/use-closure-to-protect-properties-within-an-object-from-being-modified-externally

I passed it with the code below!

var Person = function(firstAndLast) {
  // Complete the method below and implement the others similarly
  this.getFullName = function() {
    console.log(firstName + " " + lastName)
    return firstName + " " +lastName
  };

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


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

this.setFirstName = function(first) {
firstName = first
}


this.getFirstName = function() {
//console.log(firstName + "sssss")
  return firstName
}

this.setLastName = function(last) {
lastName = last
}
this.getLastName = function() {
//console.log(lastName + "sssss")
  return lastName
}




  return firstAndLast;
};

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


I would say almost there, do you ever use the fullName variable?

also, I think it would be more readable if you would define the variables at the beginning of the function