How to set fullname, firstname and lastname?

Tell us what’s happening:
When I am setting Firstname, Lastname or Fullname above getting functions are also changed. so how can i do this? Please Help

Your code so far


var Person = function(firstAndLast) {
// Complete the method below and implement the others similarly
this.getFullName = function() {
  console.log(firstAndLast)
  return firstAndLast;
};
this.getFirstName = function() {
  return firstAndLast.split(" ")[0];
}
this.getLastName = function() {
  return firstAndLast.split(" ")[1];
}
this.setFullName = function(fullName) {
  return firstAndLast = fullName;
}
// this.setFirstName = function(first) {
//   return firstAndLast.split(" ")[0] = first;
// }
// this.setLastName = function(last) {
//   return firstAndLast.split(" ")[1] = last;
// }
};

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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36.

Challenge: Make a Person

Link to the challenge:

you need to store the name inside Person, so that it can be accessed and updated as needed

I did as u told but its not working.

var Person = function(firstAndLast) {
  // Complete the method below and implement the others similarly
  var name = firstAndLast;
  this.getFullName = function() {
    console.log(firstAndLast)
    return name;
  };
  this.getFirstName = function() {
    return firstAndLast.split(" ")[0];
  }
  this.getLastName = function() {
    return firstAndLast.split(" ")[1];
  }
  this.setFullName = function(fullName) {
    console.log(name = fullName);
    return name = fullName;
  }
  // this.setFirstName = function(first) {
  //   return firstAndLast.split(" ")[0] = first;
  // }
  // this.setLastName = function(last) {
  //   return firstAndLast.split(" ")[1] = last;
  // }
};

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

do not put the variable assignment on the same line of the return statement

what do you see to say that it doesn’t work?

1 Like

Where is your output screen :thinking:? How can we see the code execution ?

I solved it. Thankyou @Anita-joseph

1 Like