Make a Person for help

Tell us what’s happening:

Why bob.getFullName() returns ‘Bob Ross’ rather than 'ff ll’

Your code so far


var Person = function(firstAndLast) {
  
  let name = firstAndLast.split(' ');
   var firstName = name[0];
  var lastName = name[1];
  var fullName = firstName + ' ' + lastName;

  this.getFullName = function() {
    return fullName;
  };
  this.getFirstName= function(){
  	return firstName;
  }; 
  this.getLastName= function(){
  	return lastName;
  };  

  this.setFirstName= function(first){
  	firstName = first;
  };  
  this.setLastName= function(last){
  	lastName = last;
  };  
  this.setFullName= function(firstAndLast){
  	fullName = firstAndLast;
  }; 

};

var bob = new Person('Bob Ross');
bob.setLastName('ll');
bob.setFirstName('ff');
console.log(bob.getFirstName());
console.log(bob.getLastName());
console.log(bob.getFullName());

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.146 Safari/537.36.

Link to the challenge:

1 Like

Neither your setLastName nor your setFiratName change the variable fullName.

fullName depends on firstName and lastName.Why did fullName not change when fullName and lastName changed?

It equals:

var firstName = Bob;
var lastName = Ross;
var fullName = firstName + ’ ’ + lastName;
firstName = ff;
lastName = ll;

So firstName and lastName changed but fullName didn’t change,right?

Because it gets initialised with those values, but nothing update fullName later on.

To give you a simple example what you are doing is similar in spirit to this:

var a = 1;
var b = 2;
var c = a + b;

console.log(a, b, c) // 1 ,2 ,3 as expected
 
a = 4;
b = 6;
console.log(a,b,c) // 4, 6, 3  -> nothing has instructed c to change

Hope it helps :sparkle:

Now I get it.Thank you!