I need help with the code. OOP concept

Tell us what’s happening:
It looks Iike my code is working fine in giving answers. But its not passing the test.
Can anyone help me explain what I’m doing wrong. please.

I did prototypes to set and get name.
Its just that I dont know what I did is wrong. Maybe illegal use of ‘this’?
Or am I using the ‘this.firstAndLast’ too much??

Your code so far


var Person = function(firstAndLast) {
this.firstAndLast = firstAndLast;
};

Person.prototype.getFirstName = function(){
let firstName = "";
for(let i = 0; i < this.firstAndLast.length; i++){
  if(this.firstAndLast[i] !== " ")
    firstName += this.firstAndLast[i];
  else
    break;
}
return firstName;
}
Person.prototype.getLastName = function(){
let lastName = this.firstAndLast.split(' ')[1];
return lastName;
}

Person.prototype.getFullName = function(){
return this.firstAndLast;
}
Person.prototype.setFullName = function(newName){
this.firstAndLast = newName;
}
Person.prototype.setFirstName = function(firstName){
let a = this.firstAndLast.split(' ');
a[0] = firstName;
this.firstAndLast = a.join(' ');
}
Person.prototype.setLastName = function(lastName){
let a = this.firstAndLast.split(' ');
a[1] = lastName;
this.firstAndLast = a.join(' ');
}


var bob = new Person('Bob Ross');
console.log(bob.getFullName());
console.log(bob.getFirstName());
console.log(bob.getLastName());
bob.setFirstName('Ryan');
bob.setLastName('luke');

console.log(bob.getFullName());

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.1 Safari/605.1.15.

Challenge: Make a Person

Link to the challenge:

Test Object.keys(bob).length should return 6. checks that the instance only have 6 methods and no other additional information. Your instance has 6 methods that work correctly, but it also has one property firstAndLast and that 7 keys in total.

thank you, but do you think my code is valid in terms of using ‘this’?

In general this challenge is somewhat broken, you should not pay a lot of attention to it. The way you’ve implemented the solution is more correct than expected by the challenge, because if we’re talking about the classes, methods should be stored inside the prototype and not directly on the instance, so Object.keys(bob).length in correct context should be 0 to start with.