In my solution I store first, last and full name all separately, 1 variable declared for each of them.
That way, when getter method is called, it’ll simply return variable, no addititonal moves needed.
const Person = function(firstAndLast) {
// Only change code below this line
// Complete the method below and implement the others similarly
//set props as local variables
let firstName = arguments[0].split(' ')[0];
let lastName = arguments[0].split(' ')[1];
let fullName = firstAndLast;
//getters
this.getFirstName = function() {
return firstName;
};
this.getLastName = function() {
return lastName;
};
this.getFullName = function() {
return fullName;
};
//setters
this.setFirstName = function(newFirst) {
firstName = newFirst;
fullName = newFirst + ' ' + lastName;
}
this.setLastName = function(newSecond) {
lastName = newSecond;
fullName = firstName + ' ' + newSecond;
}
this.setFullName = function(newFull) {
firstName = newFull.split(' ')[0];
lastName = newFull.split(' ')[1];
fullName = firstName + ' ' + lastName;
}
};
const bob = new Person('Bob Ross');
bob.getFullName();
In solution from guide, they are using only one variable to store full name, but do some splitting when getters are called.
So the question: which of these two options is more appropriate.
var Person = function(firstAndLast) {
let fullName = firstAndLast;
this.getFirstName = function() {
return fullName.split(" ")[0];
};
this.getLastName = function() {
return fullName.split(" ")[1];
};
this.getFullName = function() {
return fullName;
};
this.setFirstName = function(name) {
fullName = name + " " + fullName.split(" ")[1];
};
this.setLastName = function(name) {
fullName = fullName.split(" ")[0] + " " + name;
};
this.setFullName = function(name) {
fullName = name;
};
};
var bob = new Person("Bob Ross");
bob.getFullName();