Make a Person Challenge Error

Tell us what’s happening:
For some reason, the code below hits all the wickets except the challenge “No properties should be added. Object.keys(bob).length should always return 6.” When I run console.log(Object.keys(bob).length) I get 6 so no idea why it isn’t working.

  **Your code so far**

const Person = function (firstAndLast) {
this.getFirstName = function () {
  if (this.hasOwnProperty('fullName')) {
    return this.fullName.split(' ')[0];
  } else {
    return firstAndLast.split(' ')[0];
  }
};
this.getLastName = function () {
  if (this.hasOwnProperty('fullName')) {
    return this.fullName.split(' ')[1];
  } else {
    return firstAndLast.split(' ')[1];
  }
};
this.getFullName = function () {
  if (this.hasOwnProperty('fullName')) {
    return this.fullName;
  } else {
    return firstAndLast;
  }
};
this.setFirstName = function (name) {
  this.fullName = firstAndLast;
  this.fullName = name + ' ' + this.fullName.split(' ')[1];
};

this.setLastName = function (name) {
  this.fullName = firstAndLast;
  this.fullName = this.fullName.split(' ')[0] + ' ' + name;
};

this.setFullName = function (name) {
  this.fullName = name;
};
};
const bob = new Person('Bob Ross');

  **Your browser information:**

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.109 Safari/537.36

Challenge: Make a Person

Link to the challenge:

With your solution:

const bob = new Person('Bob Ross');

bob.setFullName('Andy Devine')

console.log(Object.keys(bob).length)
// 7

console.log(bob)
// { getFirstName: [Function],
//   getLastName: [Function],
//   getFullName: [Function],
//   setFirstName: [Function],
//   setLastName: [Function],
//   setFullName: [Function],
//   fullName: 'Andy Devine' }

You are creating another property with lines like:

  this.fullName = name;

You should be using a closure for that.

With that also, this:

  if (this.hasOwnProperty('fullName')) {
    return this.fullName;
  } else {
    return firstAndLast;
  }

can be greatly simplified - you won’t need that logic.

Excellent thank you! I was using too many this.'s

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.