What's wrong with my code? I dont see the mistakes. "Make a Person" challenge

Tell us what’s happening:
Describe your issue in detail here.
Hi!
I don’t understand why a test №9 don’t pass.

“bob.getFullName ()should return the stringHaskell Curry after bob.setLastName (“Curry”) `”

I tried to check this condition by myself on replit.com, and output is passed the test.

  **Your code so far**

var Person = function(firstAndLast) {
// Only change code below this line
// Complete the method below and implement the others similarly
this.setFirstName = function(firstName) {
  this.firstName = firstName;
  this.fullName = this.lastName ? this.firstName + " " + this.lastName : this.firstName + " " + this.getLastName();
};
this.setLastName = function(lastName) {
  this.lastName = lastName;
  this.fullName = this.firstName ? this.firstName + " " + this.lastName : this.getFirstName() + " " + this.lastName();
};
this.setFullName = function(fullName) {
  this.fullName = fullName;
  this.firstName = this.fullName.match(/\w+(?=\s)/g)[0];
  this.lastName = this.fullName.match(/(?<=\s)\w+/g)[0];
};
this.getFirstName = function() {
  return !this.firstName ? firstAndLast.match(/\w+(?=\s)/g)[0] : this.firstName;
};
this.getLastName = function() {
  return !this.lastName ? firstAndLast.match(/(?<=\s)\w+/g)[0] : this.lastName;
};
this.getFullName = function() {
  return !this.fullName ? firstAndLast : this.fullName;
};

};

var bob = new Person('Bob Ross');
bob.getFullName();
  **Your browser information:**

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

Challenge: Make a Person

Link to the challenge:

@hfe.as008 You are calling the method which is using the caller method in the method. It make errors. I would say, set the value fullName in the main scope. And you can use .split(" ")[number] instead of using inline if statements. In getFirstName and getLastName function you can use the syntax that I showed you in upper.

1 Like
  • Object.keys(bob).length should return 6.

  • bob.firstName should return undefined .

  • bob.lastName should return undefined .

These requirements mean that you cannot create this.firstName or this.lastName variables. You can create variables that are scoped inside of the Person object but are not bound to this., and that’s what I would do.

2 Likes

hi there,

//Note:

  1. for this challenge, I used the principle of ‘closure’ and ‘getter’/‘setter’ functions; using ‘getter function’ secures the original value of the variables; using ‘setter function’ allows user to change value of variables to new ones; however, these functionalities are made available only to instances of the constructor and not to public.

  2. split the '(firstAndLast) '; in that way, you’d be able to set up the variables for the ‘firstName’ and ‘lastName’

Hope you could figure out the rest. Happy coding!

1 Like

You are calling the method which is using the caller method in the method.

What’s wrong with that?

  • Object.keys(bob).length should return 6.

It returning 6

  • bob.firstName should return undefined .
  • bob.lastName should return undefined .

When these properties aren’t defined, their values are undefined.
I dont understand your thought, sorry.

Here you define this.firstName. You cannot do that, per the instructions.

Here you define this.lastName. You cannot do that, per the instructions.

Here you define this.fullName. You cannot do that, per the instructions.

Not always. Try:

var bob = new Person('Bob Ross');
bob.setFullName('Bob Ross');
console.log(Object.keys(bob).length)

You are adding keys that you were told to not add.

1 Like

Thank’s guys for all your replies!

I solved the problem. It was a syntax error in .setLastName() method.

No, its not just that. You are defining keys that you should not be.

Hint: the variable firstAndLast is captured in the closure of the Person object.

1 Like

Okay, i understand what you talking about.
It’s funny, how FCC answer checker accepts my answer =)

I tried to make these variables inside the scope of the Person object, but i dont understand how to use them inside methods. When I call method, it doesn’t overwrite values of these variables.

P.S. - sorry for my bad English.

What does this do?

var Person = function(firstAndLast) {
  // Only change code below this line
  let name = firstAndLast;
  this.printInput = function() {
    console.log(name);
  };
};

let bob = new Person('Bob Ross');
bob.printInput();
1 Like

It outputs the value of private variable “name” of the object to console.

Yep. No need to bind to this. internally.

1 Like
var Person = function(firstAndLast) {
  // Only change code below this line
  // Complete the method below and implement the others similarly
  let firstName = undefined;
  let lastName = undefined;
  let fullName = undefined;
  this.setFirstName = function(firstName2) {
    firstName = firstName2;
    fullName = lastName ? firstName + " " + lastName : firstName + " " + this.getLastName();
  };
  this.setLastName = function(lastName2) {
    lastName = lastName2;
    fullName = firstName ? firstName + " " + lastName : this.getFirstName() + " " + lastName;
  };
  this.setFullName = function(fullName2) {
    fullName = fullName2;
    firstName = fullName.match(/\w+(?=\s)/g)[0];
    lastName = fullName.match(/(?<=\s)\w+/g)[0];
  };
  this.getFirstName = function() {
    return !firstName ? firstAndLast.match(/\w+(?=\s)/g)[0] : firstName;
  };
  this.getLastName = function() {
    return !lastName ? firstAndLast.match(/(?<=\s)\w+/g)[0] : lastName;
  };
  this.getFullName = function() {
    return !fullName ? firstAndLast : fullName;
  };

};

Is there a question in there? You can make this way, way simpler by only using firstAndLast

1 Like

Is now everything right about not using “this.” for variables?

Yeah, that’s how it’s intended to work, more or less.

1 Like

I didn’t have time to refactor. I will do it, I promise)

Thank you very much for your help!
For me this mistake was not obvious, given that the system accepted my incorrect answer.

1 Like

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