Can't pass this tests , but in my com puter works fine. Help please

Tell us what’s happening:

Can’t pass this tests , but in my com puter works fine. Help please.

bob.getFullName() should return the string Haskell Curry after bob.setLastName(“Curry”).

I tested it a lot of times and it is correct and works.
Maybe another point of view can help me.
Thank you guys!

  **Your code so far**

var Person = function(firstAndLast) {
// Cambia solo el código debajo de esta línea
// Completa el método de abajo e implementa los otros de manera similar
this.getFirstName = function() {
  if(this.hasOwnProperty('firstName')) {
    return this.firstName;
  }
  return firstAndLast.split(' ')[0];
}
this.setFirstName = function(firstName) {
  this.firstName = firstName;
}
this.getLastName = function() {
  if(this.hasOwnProperty('lastName')) {
    return this.lastName;
  }
  return firstAndLast.split(' ')[1];
}
this.setLastName = function(lastName) {
  this.lastName = lastName;
}
this.getFullName = function() {
  if(this.hasOwnProperty('firstName' || this.hasOwnProperty('lastName'))) {
    return this.firstName + ' ' + (this.hasOwnProperty('lastName') ? this.lastName: firstAndLast.split(' ')[1]);
  }
  return firstAndLast;
}
this.setFullName = function(fullName) {

  this.firstName = fullName.split(' ')[0];
  this.lastName = fullName.split(' ')[1];
  return 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/91.0.4472.124 Safari/537.36

Challenge: Make a Person

Link to the challenge:

You’re mixing storing the individual names and the full name. I would pick one. This will greatly simplify your logic and fix this bug.

Thanks for answer Jeremy, but I can’t see that do you try to point me.

Either use both

and

or only use

Trying to use both is adding complexity and your bug.

1 Like

Ohhh Ok I see it. I will refactor it . Thanks a lot, Jeremy!

1 Like

This challenge is odd.

The first test should be failing with your code if I understand the intent of the challenge. It seems like the idea is to use Closures to create “private” data.

It might be seen as implementing the Module Pattern. But that fact isn’t clear at all from the requirements and the Object.keys test not failing after the creation of the instance and method invocations which can add more properties.

With your code:

var bob = new Person('Bob Ross');
console.log(Object.keys(bob).length) // 6
bob.setFullName("Haskell Curry")
console.log(Object.keys(bob).length) // 8 (the first test is still passing)

From the challenge text.

These methods must be the only available means of interacting with the object.

However with your code…

console.log(bob.firstName) // Haskell

If you look at the hint page it talks about this a bit and there is a link to an MDN article on Closures which is a hint as well. You can also look at the solution.


TL;DR I think the test for the properties is broken and should fail with your code. Unless I just don’t understand the point of the challenge.

2 Likes

Thanks you, and I agree with you about the challenge. I knew something about Closure but I will read more about closure and how to implement them with object.

Thanks both @JeremyLT and @lasjorg I just did can fixed the problem. :smiley:

1 Like

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