[SOLUTION SPOILER] Making a "person object"

Just completed the “Make a Person” challenge. My code:

var Person = function(firstAndLast) {
  // Only change code below this line
  // Complete the method below and implement the others similarly
  var nameArr = firstAndLast.split(" ");
  
  this.getFirstName = function() {
    return nameArr[0];
  };

  this.getLastName = function() {
    return nameArr[1];
  };

  this.getFullName = function() {
    return nameArr.join(" ");
  }

  this.setFirstName = function(firstInput) {
    nameArr[0] = firstInput;
  }

  this.setLastName = function(secInput) {
    nameArr[1] = secInput;
  }

  this.setFullName = function(fullInput) {
    nameArr = fullInput.split(" ");
  }

  return firstAndLast;
};

I’m still a bit uncomfortable with objects. Requesting any feedback or advice on the way I solved this. Thanks.

Your code has been blurred out to avoid spoiling a full working solution for other campers who may not yet want to see a complete solution. In the future, if you post a full passing solution to a challenge and have questions about it, please surround it with [spoiler] and [/spoiler] tags on the line above and below your solution code.

Thank you.

1 Like