Feedback needed on "Make a person" solution

Hey there :wave:t2:

I just solved the challenge, and when I looked at the solution, well… I know there are multiple ways to solve a challenge.

But I was wondering if mine presented any issues, if someone can share feedback, I’dd be grateful.

var Person = function(firstAndLast) {
  // Only change code below this line
  // Complete the method below and implement the others similarly
  this.getFullName = function () {
    return `${this.getFirstName()} ${this.getLastName()}`;
  };

  this.getFirstName = function () {
    return firstAndLast.split(" ")[0];
  };

  this.getLastName = function () {
    return firstAndLast.split(" ")[1];
  };
  this.setFirstName = function (first) {
    return (this.getFirstName = function () {
      return first;
    });
  };
  this.setLastName = function (last) {
    return (this.getLastName = function () {
      return last;
    });
  };

  this.setFullName = function (fullName) {
    return `${this.setFirstName(fullName.split(" ")[0])} ${this.setLastName(fullName.split(" ")[1])}`
  };
};

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

I think your approach is very neat. Personally I would not generally be reassigning functions in the way that you are, but I don’t know that there is anything wrong with that I am just not used to seeing it be done.

Very interesting solution. A purely functional solution with no local variables. But, to me, it is a lot more natural, clean, and error-free to keep two local variables first and last, instead of resetting a function every time a set method is called.
Also, I don’t think setFirstName and setLastName should return . It should be like

 this.setLastName = function (last) {
    this.getLastName = function () {
      return last;
    }
  };

If you have a return then I could do something like this (note: I did not verify the code)

var p = new Person("Bob Ross");
var last = p.setLastName("Smith");
p.setLastName("Jones");
console.log(p.getLastName());  //prints "Jones"
console.log(last()); //prints "Smith"

Finally, I don’t think it make sense to return a string from setFullName. It should simple reset setFirtName and setLastName.

Thanks for your reply @caryaharper. I honestly did not know how I stumbled on it, it just seemed logical at the time haha but might not be the most ideal of solutions especially when the one in the guide is much simpler :sweat_smile:

I get your point and once I had a look at the solution, I would have gone the variable route too tbh.
But to get back to your reply, console.log(last()); //prints "Smith" wouldn’t print out anything because it is not defined (ran the code for good measure) so it’s still “safe” per se.

What do you mean “not defined?” I assigned the variable last in my sample code.

let last = bob.setLastName("Smith");

I ran this code

var Person = function(lastAndLast) {
  this.getLastName = function() {
    return firstAndLast.split(" ")[1];
  }
  this.setLastName = function(last) {
    return (this.getLastName = function() {
      return last;
    });
  }
}

let bob = new Person("Bob Ross");
let last = bob.setLastName("Smith");
bob.setLastName("Johnson");

console.log(bob.getLastName());
console.log(last());

and got this output

Johnson
Smith

The set methods should normally not return anything, so the function should be

  this.setLastName = function(last) {
    this.getLastName = function() { return last; }
  }

I agree that there is no reason for the setter functions to have the return value that they do have, but you could reason that you may want them to return this

1 Like

Yes, I agree. Returning this is useful for chaining a sequence of calls.

1 Like

thank you both for your inputs, makes lot of sense :pray:t2: