Make a Person - passes test, but is this code correct?


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');

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:

How it gets stored internally is entirely up to you. It passes, because it’s perfectly valid.

Now, one thing I’d suggest – you wrote the split(’ ') code twice. Why not call setFirstAndLast() with the passed param to create the instance in the first place? That way, if the way you set a name should change, you are only changing it in the one place.

Other than that, how it is stored internally is up to you. Well done!

Your code has been blurred out to avoid spoiling a full working solution for other campers who may not yet want to see a complete solution. In the future, please do not post a full working solution. If you have a specific question about a specific section of code, post the code and your question.

Thank you.

1 Like

@snowmonkey thx for the quick response and pointer!

@RandellDawson OK will do. Is there a way I can blur out the code when posting? I want to make sure I provide enough code for responders w/o giving away too much.