Make A Person Help Please!

Alright, Ive been stuck on this one for days…https://www.freecodecamp.org/challenges/make-a-person

I have run my code for each test, and except for one, they all seem to show the correct answer. So I don’t know why the 5th - 9th tests are being marked incorrect.

Especially this one…
bob.getFullName() should return "Haskell Curry" after bob.setLastName("Curry")

I can’t wrap my brain around where in the code the name Haskell appears or is supposed to go to make this happen…

Here’s what I have, Im already quite sure it could be written in a more elegant way, my question isnt about making it better, but making it work in the first place so that I can see where my logic is wrong. Thanks!

var Person = function(firstAndLast) {
    // Complete the method below and implement the others similarly
    
    var fullName = firstAndLast;
    
    this.setFullName = function(full) {
      fullName = full;
    };
    this.setFirstName = function(first) {
    	fullName = fullName.split(" ");
    	fullName = first + " " + fullName[1];
    	return fullName;
    	
    };
    this.setLastName = function(last) {
    	fullName = fullName.split(" ");
    	fullName = fullName[1] + " " + last;
    	return fullName;
    };
    this.getFullName = function() {
     return fullName;
    };
    this.getFirstName = function() {
    	fullName = fullName.split(" ");
    	return fullName[0];
    };
    this.getLastName = function() {
    	 fullName = fullName.split(" ");
    	return fullName[1];
    };
    
    console.log(firstAndLast);
    
    return firstAndLast;
};

var bob = new Person('Bob Ross');

//bob.getFullName() should return "Haskell Curry"

bob.setLastName("Curry");

Oh jeez thanks…what an oversight! That fixed a couple of the errors… still have 3 more coming up wrong. Im looking it over some more now to see what else I missed, for sure likely something just as simple. Thanks so much!

I think I see what you mean… Check this out though…why its got me so confused. bob.getFirstName() works, but bob.getLastName() doesnt. Id get if both worked or both didnt work…but this has me flipping the table. No idea why its working or first name.but indeed makes sense I need fullName to be a string, not an array. Going to work on that now…

I think Im burning out, because what you say makes sense, but I cant seem to make sense of it in my code. Im going to take a breather…Ive been staring at this for a couple hours now. Im pretty sure when I look at it again later tonight it’ll hit me exactly what you mean and how to do it.

A small hint for you to think about. Let’s say I have the following code:

var str = "Coding is fun";

If I use split() to create an array of the words, then I could refer to the 2nd word like:

var words = str.split(" "); // does not change the original value of str
var secondWord = words[1]; 

Your code looks like:

var str = str.split(" "); // changes str into an array of words
var secondWord = str[1]; 

So, in your code, you just need to return a different variable which contains the first or last name (depending on the function), so that fullName remains a string.

Below are two possible solutions for getFirstName which you probably will not need, once you have some time to think about it, but here they are just in case. I will leave it to you to figure out getLastName.

    this.getFirstName = function() {
    	var tempArr = fullName.split(" ");
    	return tempArr[0];
    };

OR

    this.getFirstName = function() {
    	return fullName.split(" ")[0];
    };

You have the patience of a saint lol Your Coding is Fun example worked like a charm… definitely made me realize what I was doing and I did end up with what you showed in the first hidden example.

I also really needed the break… gave my pup a flea bath, which worked wonders for getting my mind off this problem for a moment :laughing: I dont know why this was so hard for me, but truly appreciate you helping me out, cause now I get it!