Make A Person Help Please!

Make A Person Help Please!
0.0 0

#1

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");

#2

Your setLastName function should be:

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

You had fullName[1] instead of fullName[0]


#3

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!


#4

The other errors are caused by two functions:

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

and

    this.getLastName = function() {
    	 fullName = fullName.split(" ");
    	return fullName[1];
    };

The question you need to ask yourself is: Should fullName be a string or an array after these functions are called?


#5

Also, while not hurting anything, do you really need the return statements in the setFirstName and setLastName?


#6

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…


#7

The reason bob.getFirstName() works, is because you split fullName (a string) and then return fullName[0] (the first name). Then when bob.getLastName() is called, you attempt to split fullName (which is no longer a string) and the script errors out, so once you stop permanently converting fullName into an array, you will be fine.


#8

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.


#9

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];
    };

#10

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!


#11

Glad I could help. I really enjoy helping others with code. Sometimes I learn something new while helping someone out, which makes it even better.