freeCodeCamp Algorithm Challenge Guide: Make a Person

freeCodeCamp Algorithm Challenge Guide: Make a Person
0

#1

:triangular_flag_on_post: Remember to use Read-Search-Ask if you get stuck. Try to pair program :busts_in_silhouette: and write your own code :pencil:

:checkered_flag: Problem Explanation:

When I started the program I figured I just had to create the six functions mentioned in the details. However, it was not that simple. Creating them as a function was not the right way, I had to create them in a different way to make them a key.

There is also a tricky part as you need six keys no more or less, so at first I had the variable that store the original name as a key too which was wrong.

As for the usage of array, that is optional, you could also create new variable to hold the separated string if you wish but an array is easier to deal with as strings are immutable.

Read the instructions carefully, it is always a good hint in itself to run the code and check what the test results were so you know what to expect but do not fixate yourself on that. Once you understand what you need to do, this problem is very easy and straightforward.

Relevant Links

:speech_balloon: Hint: 1

Use the this notation to create the keys instead of regular functions: This means instead of var varName = function() {/*...*/} you should use this.varName = function() {/*...*/}

try to solve the problem now

:speech_balloon: Hint: 2

The program has a test that checks for how many keys you used, they have to be exactly six, the six mentioned in the details section. This means if you need to work with variables, make them local and not a key: this.fullName = firstAndLast;

try to solve the problem now

:speech_balloon: Hint: 3

Often the code would not work the way you expect it due to wrong variable names, make sure to check that you spell them the right way. This happens to all of us at some point.

try to solve the problem now

Spoiler Alert!

687474703a2f2f7777772e796f75726472756d2e636f6d2f796f75726472756d2f696d616765732f323030372f31302f31302f7265645f7761726e696e675f7369676e5f322e676966.gif

Solution ahead!

:beginner: Basic Code Solution:

var Person = function(firstAndLast) {
  var fullName = firstAndLast;

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

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

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

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

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

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

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

:rocket: Run Code

Code Explanation:

  • Create a variable that will make a copy of the full name that was passed as a parameter.
  • Then we can proceed to create the six methods needed and return what is asked for.
  • For the individual setters, we can use the split to turn the fullname into an array of first and last names and concatenate the unchanged portion of the name with what was passed as a parameter.

Relevant Links

:clipboard: NOTES FOR CONTRIBUTIONS:

  • :warning: DO NOT add solutions that are similar to any existing solutions. If you think it is similar but better, then try to merge (or replace) the existing similar solution.
  • Add an explanation of your solution.
  • Categorize the solution in one of the following categories β€” Basic, Intermediate and Advanced. :traffic_light:
  • Please add your username only if you have added any relevant main contents. (:warning: DO NOT remove any existing usernames)

See :point_right: Wiki Challenge Solution Template for reference.


#2

#3

#5

…Another solution :::

var Person = function(firstAndLast) {
  var firstName,lastName;
  var splitted = function(fullName){
                   fullName = fullName.split(" ");
                   firstName = fullName[0];
                   lastName = fullName[1];
                   return firstName,lastName;
                 };
  splitted(firstAndLast);
  this.getFirstName = function (){
            return firstName;
          };
  this.getLastName = function (){
            return lastName;
          };
  this.getFullName = function (){
            return firstName + " " + lastName;
          };
  this.setFirstName = function(first){
            firstName = first;
            return firstName;
          };
  this.setLastName = function(last){
            lastName = last;
            return lastName;
          };
  this.setFullName = function(full){
            return splitted(full);
          };
};

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

#6

A more concise solution:

var Person = function(firstAndLast) {
  let fullName;
  
  this.setFullName = (full) => {fullName = full.split(' ');};
  this.setFirstName = (first) => {fullName[0] = first;};
  this.setLastName = (last) => {fullName[1] = last;};
  
  this.setFullName(firstAndLast);
  
  this.getFullName = () => fullName.join(' ');
  this.getFirstName = () => fullName[0];
  this.getLastName = () => fullName[1];
};

#7
  
  this.setFirstName = function (first) {
    var spaceIndex = firstAndLast.indexOf(" ")
    firstAndLast = firstAndLast.replace(firstAndLast.substr(0, spaceIndex), first)
  }
  this.setLastName = function (last) {
    var spaceIndex = firstAndLast.indexOf(" ")
    firstAndLast = firstAndLast.replace(firstAndLast.substr(spaceIndex + 1), last)

  }
  this.setFullName = function (newName) {
    firstAndLast = newName

  }
  this.getFullName = function () {
    return firstAndLast
  }
  this.getFirstName = function () {
    var spaceIndex = firstAndLast.indexOf(" ")
    return firstAndLast.substr(0, spaceIndex)
  }
  this.getLastName = function () {
    var spaceIndex = firstAndLast.indexOf(" ")
    return firstAndLast.substr(spaceIndex + 1)
  }
}

My code.


#8

Here is my solution:

var Person = function(firstAndLast) {
    // Complete the method below and implement the others similarly
  
  //Private variables
  //Use the value set from the public set method or the default from the constructor
  var firstName = firstName || firstAndLast.split(' ')[0], 
      lastName = lastName || firstAndLast.split(' ')[1];
  
  //Public set
  this.setFirstName = function(first){
    firstName = first; 
  };
  this.setLastName = function(last){
    lastName = last;
  };
  this.setFullName = function(fullName){
    this.setFirstName(fullName.split(' ')[0]);
    this.setLastName(fullName.split(' ')[1]);
  };
  
  //Public get
  this.getFirstName = function() {
    return firstName;
  };
  this.getLastName = function(){
    return lastName;
  };
  this.getFullName = function(){
    return this.getFirstName() + ' ' + this.getLastName();
  };
};



#9

Here is mine.


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

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

#10

Good solution.
To make code cleaner you should remove β€˜return’ values from setFIrstName and setLastName functions.


#11

My solution:

var Person = function(firstAndLast) {
var arr = firstAndLast.split(’ ');

this.setFirstName = function(i) {
  arr.splice(0, 1, i);
firstAndLast = arr.join(' ');
};
this.setLastName = function(i) {
  arr.splice(1, 1, i);
firstAndLast = arr.join(' ');
};
this.setFullName = function(i) {

var newName = i.split(’ β€˜);
arr.splice(0, 2, newName[0], newName[1]);
firstAndLast = arr.join(’ ');
};
this.getFullName = function() {
return firstAndLast;
};
this.getFirstName = function() {
return arr[0];
};
this.getLastName = function() {
return arr[1];
};
return firstAndLast;
};

var bob = new Person(β€˜Bob Ross’);


#12

Hello campers.I have a problem with getting help like we used to in the previous chat rooms where you would ask any question and get a response in due time.I kindly request someone to direct me on how to get help here.

Can someone help me complete this link:

https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bo


#13

Also,I would kindly request any of us to contribute to one resources area for External CSS,JavaScript jQuery so that any of us can access building resources.Thank you.


#14

Ohhhh this was was sweet :ok_hand::ok_hand: Good to take a breath after some hard ones!!

   var Person = function(firstAndLast) {
    // Complete the method below and implement the others similarly
    const names = firstAndLast.split(' ');
    let firstName = names[0];
    let lastName = names[1];
    
    this.getFirstName = function() {
      return firstName;
    };
  
    this.getLastName = function() {
      return lastName;
    };
    
    this.getFullName = function() {
      return `${firstName} ${lastName}`;
    };
  
    this.setFirstName = function(fname) {
      firstName = fname;
      return firstName;
    };
  
    this.setLastName = function(lname) {
      lastName = lname;
      return lastName;
    };
    
    this.setFullName = function(fullName) {
      const newFullName = fullName.split(' ');
      firstName = newFullName[0];
      lastName = newFullName[1];
      return `${firstName} ${lastName}`;
    };

};