Error? Make a Person

Error? Make a Person
0.0 0

#1

Hi. I do not understand why this is not an acceptable solution. It successfully returns every answer, but will not let me pass

Your code so far

var Person = function(firstAndLast) {
  this.setFirstName = function(first) {
    firstAndLast = firstAndLast.split(' ');
    firstAndLast.splice(0,1,first);
    firstAndLast = firstAndLast.join(' ');
  };
  this.setLastName = function(last) {
    firstAndLast = firstAndLast.split(' ');
    firstAndLast.splice(1,1,last);
    firstAndLast = firstAndLast.join(' ');
  };
  this.setFullName= function(both) {
    firstAndLast = both;
  };
  this.getFirstName = function() {
    firstAndLast = firstAndLast.split(' ');
    return firstAndLast[0];
  };
  this.getLastName = function() {
    firstAndLast = firstAndLast.split(' ');
    return firstAndLast[1];
  };
  this.getFullName = function() {
    return firstAndLast;
  };
};

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

Your browser information:

Your Browser User Agent is: ```Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:59.0) Gecko/20100101 Firefox/59.0

Link to the challenge:


#2

Can you post the full code? You’ve chopped off the first few lines.


#3

Yes, I edited the post. Thanks.


#4

The problem is you are mutating firstAndLast. When bob.getFirstName() is called, your function changes firstAndLiast from a string to an array with the following line in the this.getFirstName function.

firstAndLast = firstAndLast.split(' ');

fistAndLast is local to the entire Person function. So, when bob.getLastName() is called, you get a Type Error, because when you try to split firstAndLast again inside your getLastName function, you can not split an array with the split function. That is strictly used for splitting strings into arrays.

Then, when bob.getFullName() is called, your this.getFullName function returns an array instead of a string.


#5

OH, I see now. Thank you.