Stuck on "Make a Person"

Stuck on Intermediate Algorithm Scripting: Make a Person.

Here’s my code:


function Person(name) {
// Complete the method below and implement the others similarly
let arr = name.split(" ");

this.setFullName = function(firstAndLast) {
  if (firstAndLast) {
    this.fullName = firstAndLast;
  } else if (this.firstName && this.lastName) {
    this.fullName = this.firstName + " " + this.lastName;
  } else if (arr) {
    this.fullName = arr[0] + arr[1];
  } else {
    return "Error.";
  }
}

this.setFirstName = function(first) {
  if (first) {
    this.firstName = first;
  } else if (arr) {
    this.firstName = arr[0];
  } else if (this.fullName) {
    this.firstName = fullName.split(" ")[0];
  } else {
    return "Error.";
  }
}

this.setLastName = function(last) {
  if (last) {
    this.lastName = last;
  } else if (arr) {
    this.lastName = arr[1];
  } else if (this.fullName) {
    this.lastName = fullName.split(" ")[1];
  } else {
    return "Error.";
  }
}

this.getFullName = function() {
  if (this.fullName) {
    return this.fullName;
  } else if (this.firstName && this.lastName) {
    return this.firstName.concat(this.lastName);
  } else {
    return undefined;
  };
}

this.getFirstName = function() {
  if (this.firstName) {
    return this.firstName;
  } else if (this.fullName) {
    return this.fullName.split(" ")[0];
  } else {
    return undefined;
  };
}

this.getLastName = function() {
  if (this.lastName) {
    return this.lastName;
  } else if (this.fullName) {
    return this.fullName.split(" ")[1];
  } else {
    return undefined;
  };
}
}

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

And the console returns:

I get the feeling my code got way out of hand, but I was trying to assign everything based on one of four single arguments, depending on the function being called. Help?

Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64; rv:77.0) Gecko/20100101 Firefox/77.0.

Challenge: Make a Person

Link to the challenge:

You’re overthinking it.

You should have six methods, and all those methods need to do is interact with the name parameter to the Person constructor in some way. You don’t need any if/else statements to do this.

  • getFirstName() should return the first word in name.
  • getLastName() should return the second word in name.
  • getFullName() should return name.
  • setFirstName(firstName) should reassign name so that the first word is firstName and the second word is unchanged.
  • setLastName(lastName) should reassign name so that the second word is lastName and the first word is unchanged.
  • setFullName(fullName) should reassign name to the value of fullName.
1 Like

Note the first test case:

Object.keys(bob).length should return 6.

You cannot set any properties or methods other than those 6 mentioned in the instructions. In your current solution, you’re making 4 additional properties – firstName, lastName, fullName, and arr.

Nope, It’s not :wink:

Finally figured out what I was doing wrong. This helped a lot. Thank you!

I eventually got it to work. But I noticed toward the end, when I had a working code, and knew it worked, I was still getting two errors. Turned out it was because I had changed the function call at the bottom when I was running tests. So to be safe, make sure you remove any test calls.

1 Like

Your last comment here was key for me. Thank you so much for sharing!

1 Like

Good job! Sometimes you just need to step away from the problem for some time and return with a clear mind.

1 Like