Hi,
For the Make a Person challenge, I passed the test, but I’m wondering if my code is “kosher” … specifically, storing first and last name in an array variable, and having the methods reference that array.
Any downsides to this? Should I have instead included all code only in the methods with no reference to variables outside the methods?
My code:
var Person = function(firstAndLast) {
// is it bad form to use this array?
var nameArr = firstAndLast.split(' ');
// functions
this.getFullName = function() {
return (nameArr[0] + ' ' + nameArr[1]);
};
this.getFirstName = function() {
return nameArr[0];
};
this.getLastName = function() {
return nameArr[1];
};
this.setFirstName = function(first) {
nameArr[0] = first;
}
this.setLastName = function(last) {
nameArr[1] = last;
}
this.setFullName = function(firstAndLast) {
let arr = firstAndLast.split(' ');
nameArr[0] = arr[0];
nameArr[1] = arr[1];
}
};
var bob = new Person('Bob Ross');
bob.getFullName();
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36
.
Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/make-a-person