Intermediate Algorithm Scripting - Make a Person

Tell us what’s happening:
Why I can’t store fullName.split( ) into variable and make it reusable in this code? I get an error.
Is this because fullName can be changed later as we execute like setFirstName function?

And I can’t understand why the order of executing is matter here(correct solution not my wrong code). For example,
const bob = new Person(“Bob Ross”);
console.log(bob.setFirstName(“Haskell”)) //undefined
-why is it undefined?
console.log(bob.getFullName()); //Haskell Rossed
-why is it not Bob Ross?

I’m beginner so I would be grateful if someone could teach me the reason. Thank you.

Your code so far
const Person = function(firstAndLast) {
let fullName = firstAndLast;
let splitName = fullName.split(``)

this.getFirstName = function() {
return splitName[0];
};

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

this.getFullName = function() {
return fullName;
};

this.setFirstName = function(first) {
fullName = first + " " + splitName[1];
};

this.setLastName = function(last) {
fullName = splitName[0] + " " + last;
};

this.setFullName = function(newName) {
fullName = newName;
};
};

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36

Challenge: Intermediate Algorithm Scripting - Make a Person

Link to the challenge:

Are you really using an empty string in this split? You might want to console.log splitName to see what you are doing.

Sorry I forgot to put space in quote when I posted this code. But I put space in quote like fullName.split(" ") . I get following two error.

bob.getFirstName() should return the string Haskell after bob.setFullName("Haskell Curry").

bob.getLastName() should return the string Curry after bob.setFullName("Haskell Curry").
this.setFirstName = function (first) {
    fullName = first + " " + splitName[1];
};

Above you are setting the variable fullName to update the first name, which is fine.

this.getFirstName = function () {
    return splitName[0];
};

But here you are accessing the variable splitName to return the first name. But where does splitName ever get updated in your class to reflect an updated first name?

I’m still confused and I think I still don’t understand class properly or misunderstand something. I will reply when I understand what you are pointing out. Thank you for your time and reply. I appreciate it.

Just keep in mind that this:

let splitName = fullName.split(` `);

Is only executed once (when you initially create the object).

Thank you! I’ll keep that in mind.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.