Javascript "Make a Person" exercise

Tell us what’s happening:
I wonder why my code fails to pass the test, I have tested it manually and apparently, it works.

I have printed every test result to compare to the provided ones and I get the results I am supposed to, but the testing machine is not approving my code to pass items from 5 to 8 ->

bob.getFirstName() should return “Bob”.

bob.getLastName() should return “Ross”.

bob.getFullName() should return “Bob Ross”.

bob.getFullName() should return “Haskell Ross” after bob.setFirstName("Haskell")

I have tried to compare those results to what I am getting through localeCompare() function and I get that both values are equals (0), so I don’t understand why the test fails.

Your code so far


var Person = function(firstAndLast) {
var name = firstAndLast.split(" ");
this.getFirstName = function() {
  return name[0];
};

this.getLastName = function() {
  return name[1];
};

this.getFullName = function() {
  return name.join(" ");
};

this.setFirstName = function(first) {
  name[0] = first;
};

this.setLastName = function(last) {
  name[1] = last;
};

this.setFullName = function(firstAndLast) {
  name[0] = firstAndLast.split(" ")[0];
  name[1] = firstAndLast.split(" ")[1];
};
};

var bob = new Person('Bob Ross');
console.log(Object.keys(bob).length);
console.log(bob instanceof Person);
console.log(bob.firstName);
console.log(bob.lastName);
console.log(bob.getFirstName());
console.log(bob.getLastName());
console.log(bob.getFullName());
bob.setFirstName("Haskell");
console.log(bob.getFullName());
bob.setLastName("Curry");
console.log(bob.getFullName());
bob.setFullName("Haskell Curry");
console.log(bob.getFullName());
console.log(bob.getFirstName());
console.log(bob.getLastName());

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:76.0) Gecko/20100101 Firefox/76.0.

Challenge: Make a Person

Link to the challenge:

Hello!

This took me a moment, but I got it.

console.log(Object.keys(bob).length);
console.log(bob instanceof Person);
console.log(bob.firstName);
console.log(bob.lastName);
console.log(bob.getFirstName());
console.log(bob.getLastName());
console.log(bob.getFullName());
bob.setFirstName("Haskell");
console.log(bob.getFullName());
bob.setLastName("Curry");
console.log(bob.getFullName());
bob.setFullName("Haskell Curry");
console.log(bob.getFullName());
console.log(bob.getFirstName());
console.log(bob.getLastName());

Delete all of this. It’s setting your bob object and the tests don’t reset it before running.

Thanks a lot. It worked!

1 Like