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: