It works on node.js but doesn't pass the tests, and I can't figure why?

Hi!

Could you please take a look and tell me why it doesn’t pass the test, even though it works fine in node.js?

https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/make-a-person

var Person = function(firstAndLast) {
  // Complete the method below and implement the others similarly
  this.getFullName = function() {
    return this.getFirstName() + ' ' + this.getLastName();
  };
  this.getFirstName = function () {
    return `Bob`;
  }
  
  this.getLastName = function () {
    return `Ross`;
  }

  this.setFullName = function(fullName) {
    this.getFullName = function() {
      return fullName;
    }
    this.setFirstName(fullName.split(' ')[0]);
    this.setLastName(fullName.split(' ')[1]);
  }

  this.setFirstName = function(firstName) {
    this.getFirstName = function() {
      return firstName;
    }
  }

  this.setLastName = function(lastName) {
    this.getLastName = function() {
      return lastName;
    }
  }  

};

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

if (Object.keys(bob).length !== 6) {
  console.log('Object.keys(bob).length should return 6.');
};

if ( ! (bob instanceof Person)) {
  console.log('bob instanceof Person should return true.');
}

if (bob.firstName !== undefined) {
  console.log('bob.firstName should return undefined.');
}

if (bob.lastName !== undefined) {
  console.log('bob.lastName should return undefined.');
}

if (bob.getFirstName() !== 'Bob') {
  console.log('bob.getFirstName() should return "Bob".');
}

if (bob.getLastName() !== 'Ross') {
  console.log('bob.getLastName() should return "Ross".');
}

if (bob.getFullName() !== 'Bob Ross') {
  console.log('bob.getFullName() should return "Bob Ross".');
}

bob.setFirstName('Haskell');

if (bob.getFullName() !== 'Haskell Ross') {
  console.log('bob.getFullName() should return "Haskell Ross".');
}

bob.setLastName('Curry');

if (bob.getFullName() !== 'Haskell Curry') {
  console.log('bob.getFullName() should return "Haskell Curry".');
}

bob.setFullName('Haskell Curry');

if (bob.getFullName() !== 'Haskell Curry') {
  console.log('bob.getFullName() should return "Haskell Curry".');
}


Your Person should not only be Bob Ross. You should be able to create a Person with any name. Your getFirstName will only ever return “Bob”. Your getLastName will only ever return “Ross”. That means that your getFullName and setFullName will always return “Bob Ross”.

If this was the case this check would fail, but it didn’t.

Yup, that’s right but this is not the matter of my question.

After bob.setFullName('Haskell Curry') getFullName() returns ‘Haskell Curry’. This should pass through the test but it doesn’t and I wonder why.

I pointing out that your code does not execute correctly. It can only create “Bob Ross”.

An additional problem is that you are referencing variables that don’t exist. You return fullName, firstName and lastName, but where are those variables declared?

@diechtiar @ArielLeslie is spot on. You are assuming the tests are always using the original bob instance. You need to adjust your solution to work with any first and last name separated by a space character that would get passed to the Person function.

The test you are failing creates a new person:

var _bob=new Person('Haskell Ross');
_bob.setLastName("Curry");
_bob.getFullName(); // returns "Bob Curry" instead of "Haskell Curry"

To make it clearer to the camper, I think we should probably explain in the test that a new instance is created for the test. I will create a new issue, so it can be discussed.

1 Like

note that getFirstName() and getLastName() are changed by setFullName() to something more useful than return 'Bob' and return 'Ross'. The code passes all the tests except one, which is puzzling. But thanks for your input, I’ll refactor this.

This is the case. I made exactly this assumption that the bob instance is used throughout the test. It’s all clear now! Thank you!