Intermediate Algorithm Scripting - Make a Person

Tell us what’s happening:
I asked chatgpt how to make properties private, and it gave me an example using closure. My solution looks good and passed all tests but one.

You should be able to instantiate your Person object.

But I was able to create an instance of Person.

Your code so far

const Person = function(first, last) {
  let firstname = first;
  let lastname = last;

  return {
    getFirstName: function() {
      return firstname;
    },
    getLastName: function() {
      return lastname;
    },
    getFullName: function() {
      return firstname +" " +lastname;
    },
    setFirstName: function(newFirst) {
      firstname = newFirst;
    },
    setLastName: function(newLast) {
      lastname = newLast;
    },
    setFullName: function(newFirst, newLast) {
      firstname = newFirst;
      lastname = newLast;
    }
  };
};

let p = new Person('Bob', 'Ross');
console.log(Object.keys(p));

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36

Challenge: Intermediate Algorithm Scripting - Make a Person

Link to the challenge:

What is this syntax? Its not something you’ve seen in the freeCodeCamp coursework.

I’d stick to syntax covered in the course. In particular, you should follow the starting code:

const Person = function(first, last) {
  this.getFullName = function() {
    return "";
  };
  return "";
};
1 Like

Thanks! I finished it with this framework.
One more question. What does the return ""; at the bottom do? It doesn’t seem to do anything as I tried removing it.

I’m not sure about that part. I don’t think the return value of a constructor defined in this way actually matters?

1 Like

"Return a new constructed object, rather than the default binding“

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