What am I not seeing? Make a person challenge

Im trying to solve the intermediate algorithm scripting :Make a Person challenge and while i seem to know where the problem is , I cant seem to understand why my initial solution doesnt work. Heres my code:

var Person = function(firstAndLast) {
  let fullName = firstAndLast.split(" ") ;
  let firstName = fullName[0];
  let lastName = fullName[1];
  this.setFirstName = function(newFirstName){
    fullName[0] = newFirstName;
  this.setLastName = function(newLastName){
    fullName[1]  = newLastName;
  this.setFullName = function(newFullName){
    fullName = newFullName.split(" ");
  this.getFirstName = function() {
    return firstName;},
  this.getLastName = function() {
    return lastName;},
  this.getFullName = function() {
    return fullName.join(" ");}
var bob = new Person('Bob Ross');

I keep failing the last two tests and after some testing with console logs Ive found that that changing the get methods to :

this.getFirstName = function() {
    return fullName[0];},
this.getLastName = function() {
    return fullName[1];},

succesfully passes all tests, but I dont understand why?

firstName and lastName are never updated, so they keep having the original value

1 Like