Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save SerhiiLihus/0ebd140d478947148e9b to your computer and use it in GitHub Desktop.

Select an option

Save SerhiiLihus/0ebd140d478947148e9b to your computer and use it in GitHub Desktop.
Fill in the object constructor with the methods specified in the tests. Those methods are getFirstName(), getLastName(), getFullName(), setFirstName(first), setLastName(last), and setFullName(firstAndLast). These methods must be the only available means for interacting with the object.
// Bonfire: Make a Person(Upper Intermediate Algorithm)
// Author: @serhiilihus
// http://www.freecodecamp.com/challenges/bonfire-make-a-person#?solution=var%20Person%20%3D%20function(firstAndLast)%20%7B%0A%20%20var%20fullName%20%3D%20firstAndLast%3B%0A%20%20this.getFullName%20%3D%20function()%20%7B%0A%20%20%20%20return%20fullName%3B%0A%20%20%7D%3B%0A%20%20this.getFirstName%20%3D%20function()%20%7B%0A%20%20%20%20return%20fullName.split(%22%20%22)%5B0%5D%3B%0A%20%20%7D%3B%0A%20%20this.getLastName%20%3D%20function()%20%7B%0A%20%20%20%20return%20fullName.split(%22%20%22)%5B1%5D%3B%0A%20%20%7D%3B%0A%20%20this.setFirstName%20%3D%20function(first)%20%7B%0A%20%20%20%20this.setFullName(first%20%2B%20%22%20%22%20%2B%20fullName.split(%22%20%22)%5B1%5D)%3B%0A%20%20%7D%3B%0A%20%20this.setLastName%20%3D%20function(last)%20%7B%0A%20%20%20%20this.setFullName(fullName.split(%22%20%22)%5B0%5D%20%2B%20%22%20%22%20%2B%20last)%3B%0A%20%20%7D%3B%0A%20%20this.setFullName%20%3D%20function(firstAndLast)%20%7B%0A%20%20%20%20fullName%20%3D%20firstAndLast%3B%0A%20%20%7D%3B%0A%7D%3B%0A%0Avar%20bob%20%3D%20new%20Person(%27Bob%20Ross%27)%3B%0Abob.getFullName()%3B%0A
// Learn to Code at Free Code Camp (www.freecodecamp.com
var Person = function(firstAndLast) {
var fullName = firstAndLast;
this.getFullName = function() {
return fullName;
};
this.getFirstName = function() {
return fullName.split(" ")[0];
};
this.getLastName = function() {
return fullName.split(" ")[1];
};
this.setFirstName = function(first) {
this.setFullName(first + " " + fullName.split(" ")[1]);
};
this.setLastName = function(last) {
this.setFullName(fullName.split(" ")[0] + " " + last);
};
this.setFullName = function(firstAndLast) {
fullName = firstAndLast;
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment