Make a Person new

Please help !!! I can not understand why some tests do not pass


var Person = function(firstAndLast) {
  // Complete the method below and implement the others similarly
  let inputData = firstAndLast.match( /\w+/ig );
  let firstName = inputData[0];
  let lastName = inputData[1];
  let fullName = firstName + " " + lastName;


  this.getFirstName = function() {
    return firstName;
  };
  this.getLastName = function() {
    return lastName;
  };
  this.getFullName = function() {
    return fullName;
  };
  this.setFirstName = function(setFirstName) {
    return firstName = setFirstName;
  };
  this.setLastName = function(setLastName) {
    return lastName = setLastName;
  };
  this.setFullName = function(setFullName) {
    let arrSetFullName = setFullName.match( /\w+/ig );
    return fullName = arrSetFullName.join(" ");
  };
  return firstAndLast;
};

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

bob.getFullName();

Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36.

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

When you are setting firstnames and lastnames, you need to also update your fullname.

1 Like

I changed the code a little bit, but I haven’t passed two tests yet.

bob.getFullName() should return “Haskell Curry” after bob.setFullName("Haskell Curry") .
bob.getLastName() should return “Curry” after bob.setFullName("Haskell Curry") .

var Person = function(firstAndLast) {
  // Complete the method below and implement the others similarly
  let inputData = firstAndLast.match( /\w+/ig );
  let firstName = inputData[0];
  let lastName = inputData[1];
  let fullName;

  this.getFirstName = function() {
    return firstName;
  };
  this.getLastName = function() {
    return lastName;
  };
  this.getFullName = function() {
    return fullName = firstName + " " + lastName;
  };
  this.setFirstName = function(first) {
    return firstName = first;
  };
  this.setLastName = function(last) {
    return lastName = last;
  };
  this.setFullName = function(firstAndLast) {
    let arrSetFullName = firstAndLast.match( /\w+/ig );
    return fullName = arrSetFullName.join(" ");
  };
  return firstAndLast;
};

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

bob.getFullName();```

Your setFirstName and setLastName methods shouldn’t return anything. That’s the purpose of your get methods. They are literally setting/updating values in your constructor. When you are updating either first/last name make sure to update fullName as well because fullName will always change if firstName or lastName changes.

1 Like

Adding to what @shimphillip has already said above, your getFullName involves the firstName and lastName variables, so if that is the case you coulld have your setFullName update the firstName and lastName variables.

1 Like

@wayper Make sure on all your “set” methods you are not returning a value. Only your “get” methods should be returning a value. Lastly, since you are using firstName and lastName mostly in your Person function, I would get rid of the fullName variable all together and just use firstName and lastName to build the string to return for the getFullName method and then for the setFullName method, you would just need to set the firstName and lastName from the arrSetFullName array.

EDIT: Also, you don’t need the following line at the end because the methods take care of anything which needs to be returned.

return firstAndLast;
1 Like

Many thanks for the advice. Happened