Skip to content

Instantly share code, notes, and snippets.

@martin-mok
Last active January 13, 2020 20:16
Show Gist options
  • Select an option

  • Save martin-mok/a012c59e4c74ecb74703633e8414e29a to your computer and use it in GitHub Desktop.

Select an option

Save martin-mok/a012c59e4c74ecb74703633e8414e29a to your computer and use it in GitHub Desktop.
freecodecamp: Make a Person javascript object initialization

Description

Fill in the object constructor with the following methods below:
getFirstName()
getLastName()
getFullName()
setFirstName(first)
setLastName(last)
setFullName(firstAndLast)

Run the tests to see the expected output for each method. The methods that take an argument must accept only one argument and it has to be a string. These methods must be the only available means of interacting with the object.

Tests

tests:
  - text: <code>Object.keys(bob).length</code> should return 6.
    testString: assert.deepEqual(Object.keys(bob).length, 6);
  - text: <code>bob instanceof Person</code> should return true.
    testString: assert.deepEqual(bob instanceof Person, true);
  - text: <code>bob.firstName</code> should return undefined.
    testString: assert.deepEqual(bob.firstName, undefined);
  - text: <code>bob.lastName</code> should return undefined.
    testString: assert.deepEqual(bob.lastName, undefined);
  - text: <code>bob.getFirstName()</code> should return "Bob".
    testString: assert.deepEqual(bob.getFirstName(), 'Bob');
  - text: <code>bob.getLastName()</code> should return "Ross".
    testString: assert.deepEqual(bob.getLastName(), 'Ross');
  - text: <code>bob.getFullName()</code> should return "Bob Ross".
    testString: assert.deepEqual(bob.getFullName(), 'Bob Ross');
  - text: <code>bob.getFullName()</code> should return "Haskell Ross" after <code>bob.setFirstName("Haskell")</code>.
    testString: assert.strictEqual((function () { bob.setFirstName("Haskell"); return bob.getFullName(); })(), 'Haskell Ross');
  - text: <code>bob.getFullName()</code> should return "Haskell Curry" after <code>bob.setLastName("Curry")</code>.
    testString: assert.strictEqual((function () { var _bob=new Person('Haskell Ross'); _bob.setLastName("Curry"); return _bob.getFullName(); })(), 'Haskell Curry');
  - text: <code>bob.getFullName()</code> should return "Haskell Curry" after <code>bob.setFullName("Haskell Curry")</code>.
    testString: assert.strictEqual((function () { bob.setFullName("Haskell Curry"); return bob.getFullName(); })(), 'Haskell Curry');
  - text: <code>bob.getFirstName()</code> should return "Haskell" after <code>bob.setFullName("Haskell Curry")</code>.
    testString: assert.strictEqual((function () { bob.setFullName("Haskell Curry"); return bob.getFirstName(); })(), 'Haskell');
  - text: <code>bob.getLastName()</code> should return "Curry" after <code>bob.setFullName("Haskell Curry")</code>.
    testString: assert.strictEqual((function () { bob.setFullName("Haskell Curry"); return bob.getLastName(); })(), 'Curry');

Solution

Offical Soln:

var Person = function(firstAndLast) {

  var firstName, lastName;

  function updateName(str) {
    firstName = str.split(" ")[0];
    lastName = str.split(" ")[1];
  }

  updateName(firstAndLast);

  this.getFirstName = function(){
    return firstName;
  };

  this.getLastName = function(){
    return lastName;
  };

  this.getFullName = function(){
    return firstName + " " + lastName;
  };

  this.setFirstName = function(str){
    firstName = str;
  };


  this.setLastName = function(str){
    lastName = str;
  };

  this.setFullName = function(str){
    updateName(str);
  };
};

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

My soln:

var Person = function(firstAndLast) {
let firstName="";
let lastName="";

if(typeof firstAndLast !=="string")  {
      return undefined;
    }
[firstName,lastName]=firstAndLast.split(" ");

this.getFirstName=function(){
  return firstName;
};
this.getLastName=function(){
return lastName;
};
this.getFullName=function(){
return firstName+" "+lastName;
};
this.setFirstName=function(first){
firstName=first;
};
this.setLastName=function(last){
lastName=last;
};
this.setFullName=function(firstAndLast){
  let name=firstAndLast.split(" ");
firstName=name[0];
lastName=name[1];
};
};

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

Learn

private variables

In JavaScript, a function always has access to the context in which it was created. This is called closure.
private variables can be created by changing

//don't use this
this.a;
//use let, const, var
let a;

which let the closure to protect the variables.

initializing object

constructor in Javascript is used to define "class".
It is not the same as constructor method in Java/C++/C#/Python class, in which, constructor is used to Initialize object.
To initialize an object in Javascript, we can just write the initialization statements inside the constructor function,
or better create a "initialization inner function", and call it inside the constructor function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment