Make a Person algorytm excersise

Continuing the discussion from freeCodeCamp Challenge Guide: Make a Person:

Me thinks storing the fullName as array would benefit from less splitting.
var Person = function(firstAndLast) {
this.fullName = [“First”, “Last”];
this.setFirstName = (first)=>{ this.fullName[0] = first; },
this.setLastName = (last)=>{ this.fullName[1] = last; },
this.setFullName = (full)=>{
this.setFirstName(full.split(" “)[0];
this.setLastName(full.split(” ")[1];
},

this.getFirstName = ()=>{ return this.fullName[0] ; },
this.getLastName = ()=>{ return this.fullName[1] ; },
this,getFullName = ()=> { return this.fullName.join(" ") ; }

/*
ofcourse:
this._init = function(firstAndLast){
this.setFullName(firstAndLast)
}
*/
}

Can someone explain how this is worse than the expected answer?

Does your solution pass tests?

Nope, Even the proposed answer doesn’t pass. I’m skipping this one. Lost too much time

the solution in the guide passes

reasons for which your code doesn’t pass:

  • you hardcode starting value for the name, instead of using the input
  • fullName shouldn’t be an object property, but a local variable

I copied the answer from the 'bot. That didn’t pass. My question here is more about why repeating “split” several times which I think is unnecessary.
I know my solution won’t pass , it doesn’t meet the criteria. However, it does explain why I’m regularly adressed as Mr. Van, because this way of dealing with names doesn’t take into consideration last names with a space.

this shouldn’t at all be the way of dealing with names

about split(), it depends on the way you choose to structure it. this would need some brainstorming to make it DRY (don’t repeat yourself), but at the level at which this challenge is presented, I don’t think it needs that much work. for the setFullName method, it would be possible to use less split methods with just fullName = full.split(' ')


about names, this could be a good read:
https://www.kalzumeus.com/2010/06/17/falsehoods-programmers-believe-about-names/

(for more falsehoods there is this repo: https://github.com/kdeldycke/awesome-falsehood)

1 Like

You are exactly right about your concerns. Often tutorials have a particular aim, and they provide simplifications of things for the purposes of having solutions that can be tested for. They shouldn’t be misconstrued as the basis for a real system. As a long time system architect who has done a lifetime worth of data modelling, there is a simple and common way to model the data for a person. The field naming does assume western conventions in terms of surname position, but it requires at least these separate columns:

  • Prefix (Mr. Mrs. Ms., Dr etc.)
  • Firstname
  • Middle Name or Initial
  • Lastname
  • Suffix (Jr. Sr. 3rd, etc.) [Optional, can be kept as part of Lastname]

So without a doubt these need to be kept as separate attributes, and can and should be used to compute a fullname in various forms.

1 Like

Thanks for your input. and the funny links. :smiley:
Don’t get me started om i18n. I just hate how, based on my IP adress’ location, I’m supposed to speak the local lingo. Why don’t they just look at at the browser’s language settings.

I have a db background too, and I’m always looking to restrict the users free input to an absolute minimum.
I have a Dutch name, so in your example I would use not prefix, but title, and prefix for the last name prefix (in my case -‘van’-, not capatalised).

Or maybe let it all go and give 'em 1 box in which they can put everything and forget about name-parts. :wink: :thinking:

Thanks for the feedback , much appreciated. :+1:

In terms of your suggestion, it absolutely makes sense for this particular challenge and you’re absolutely right. But you still have to write it in the way it passes the tests (regardless of how unrealistic this whole challenge is)

I hope my example didn’t lead to confusion. The prefix was a way of indicating how a person would formally be addressed, and is in no way part of the name. A person with a name like Richard Van Dyke would have:

firstname: Richard
lastname: Van Dyke