Make a Person - issues with setting first name and last name

Tell us what’s happening:
So I don’t really understand what’s going wrong. For some reason setFirstName and setLastName aren’t working properly but I’m also confused the the Object.keys(bob)
.length is only returning 5 instead of 6.
Your code so far

const Person = function(firstAndLast) {
  
  this.getFullName = function() {
    return firstAndLast;
  };
  
  this.getFirstName = function(){
    let first = firstAndLast.split(' ');
    return first[0];
  }
  
  this.getLastName = function(){
    let last = firstAndLast.split(' ');
    return last[1];
  }
  
  this.setFirstName = function (firstName){
  let first = firstAndLast.split(' ');
  first[0] = firstName;
  return first.join(' ');
};

  this.setFirstName = function (lastName){
  let last = firstAndLast.split(' ');
  last[1] = lastName;
  return last.join(' ');
};

this.setFullName = function (input){
    return  firstAndLast = input;
};

};

const bob = new Person('Bob Ross');
bob.setFirstName("Haskell");
console.log(bob.getFullName());
console.log(Object.keys(bob).length);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/114.0

Challenge: Intermediate Algorithm Scripting - Make a Person

Link to the challenge:

Look at the output of your last console log:

console.log(Object.keys(bob).length);

It’s printing 5 to the console but the tests say there should be six.

this.setFirstName = function (firstName) {
    let first = firstAndLast.split(' ');
    first[0] = firstName;
    return first.join(' ');
  };

  this.setFirstName = function (lastName) {
    let last = firstAndLast.split(' ');
    last[1] = lastName;
    return last.join(' ');
  };

You’ve defined setFirstName twice. I’m guessing one of these should be setLastName.

Also, setFirstName is working on local variables for that method, so any changes there won’t persist. For example, if I called bob.setFirstName('Todd') then if I immediately called bob.getFirstName it should return Todd, but it won’t because your getFirstName method is working on the original firstAndLast variable passed into the function. This variable wasn’t updated in setFirstName, so getFirstName won’t have access to the correct first name.

1 Like

Thanks so much. I can’t believe I didn’t notice I didn’t change the firstName to Last lol. I just copied the first function after I wrote it cause I didn’t wanna rewrite it again, I was staring at it for like 30 minutes trying to make sure I didn’t spell anything wrong. And you were right I realized that what I was returning at the end of the function was firstAndLast and not the joined split array . Anyway I got it so thank you very much for your help and advice. :slight_smile:

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.