Stuck on Intermediate Algorithm Scripting: Make a Person.
Here’s my code:
function Person(name) {
// Complete the method below and implement the others similarly
let arr = name.split(" ");
this.setFullName = function(firstAndLast) {
if (firstAndLast) {
this.fullName = firstAndLast;
} else if (this.firstName && this.lastName) {
this.fullName = this.firstName + " " + this.lastName;
} else if (arr) {
this.fullName = arr[0] + arr[1];
} else {
return "Error.";
}
}
this.setFirstName = function(first) {
if (first) {
this.firstName = first;
} else if (arr) {
this.firstName = arr[0];
} else if (this.fullName) {
this.firstName = fullName.split(" ")[0];
} else {
return "Error.";
}
}
this.setLastName = function(last) {
if (last) {
this.lastName = last;
} else if (arr) {
this.lastName = arr[1];
} else if (this.fullName) {
this.lastName = fullName.split(" ")[1];
} else {
return "Error.";
}
}
this.getFullName = function() {
if (this.fullName) {
return this.fullName;
} else if (this.firstName && this.lastName) {
return this.firstName.concat(this.lastName);
} else {
return undefined;
};
}
this.getFirstName = function() {
if (this.firstName) {
return this.firstName;
} else if (this.fullName) {
return this.fullName.split(" ")[0];
} else {
return undefined;
};
}
this.getLastName = function() {
if (this.lastName) {
return this.lastName;
} else if (this.fullName) {
return this.fullName.split(" ")[1];
} else {
return undefined;
};
}
}
var bob = new Person('Bob Ross');
console.log(bob.getFullName());
And the console returns:
I get the feeling my code got way out of hand, but I was trying to assign everything based on one of four single arguments, depending on the function being called. Help?
Your browser information:
User Agent is: Mozilla/5.0 (X11; Linux x86_64; rv:77.0) Gecko/20100101 Firefox/77.0
.
Challenge: Make a Person
Link to the challenge: