Tell us what’s happening:
I don’t understand why:
-
my .split, .splice and .join methods don’t work here… I’m getting weird results with .splice not working at all as expected.
-
Why do I have to declare and store firstAndLast value in a variable and can’t just use firstAndLast as it is ? I can see it’s stored in created object, so why can’t use it, overwrite it etc ?
Your code so far
//THIS DOEN"T WORK
const Person = function(firstAndLast) {
// Only change code below this line
// Complete the method below and implement the others similarly
// let fullName = firstAndLast
this.getFullName = function() {
return firstAndLast;
};
this.getFirstName = function(){
return firstAndLast.split(" ")[0]
}
this.getLastName = function(){
return firstAndLast.split(" ")[1]
}
//PROBLEMATIC AREA ------------------------------
this.setFirstName = function(first){
// firstAndLast = first + " " + firstAndLast.split(" ")[1]
firstAndLast = firstAndLast.split(" ").splice(0,1,first).join(" ")
console.log(firstAndLast)
}
//PROBLEMATIC AREA ------------------------------
};
const bob = new Person('Bob Ross');
bob.setFirstName("Haskell")
console.log(bob.getFirstName());
//THIS DOES WORK
const Person = function(firstAndLast) {
// Only change code below this line
// Complete the method below and implement the others similarly
// let fullName = firstAndLast
this.getFullName = function() {
return firstAndLast;
};
this.getFirstName = function(){
return firstAndLast.split(" ")[0]
}
this.getLastName = function(){
return firstAndLast.split(" ")[1]
}
//PROBLEMATIC AREA ------------------------------
this.setFirstName = function(first){
firstAndLast = first + " " + firstAndLast.split(" ")[1]
console.log(firstAndLast)
}
//PROBLEMATIC AREA ------------------------------
};
const bob = new Person('Bob Ross');
bob.setFirstName("Haskell")
console.log(bob.getFirstName());
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:108.0) Gecko/20100101 Firefox/108.0
Challenge: Intermediate Algorithm Scripting - Make a Person
Link to the challenge: