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');
bob.getFullName();
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?