Advertisement
svetlai

Person - Adri

Jun 17th, 2015
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* Task Description */
  2. /*
  3.  Create a function constructor for Person. Each Person must have:
  4.  *  properties `firstname`, `lastname` and `age`
  5.  *  firstname and lastname must always be strings between 3 and 20 characters, containing only Latin letters
  6.  *  age must always be a number in the range 0 150
  7.  *  the setter of age can receive a convertible-to-number value
  8.  *  if any of the above is not met, throw Error
  9.  *  property `fullname`
  10.  *  the getter returns a string in the format 'FIRST_NAME LAST_NAME'
  11.  *  the setter receives a string is the format 'FIRST_NAME LAST_NAME'
  12.  *  it must parse it and set `firstname` and `lastname`
  13.  *  method `introduce()` that returns a string in the format 'Hello! My name is FULL_NAME and I am AGE-years-old'
  14.  *  all methods and properties must be attached to the prototype of the Person
  15.  *  all methods and property setters must return this, if they are not supposed to return other value
  16.  *  enables method-chaining
  17.  */
  18. function solve() {
  19.     var Person = (function () {
  20.         function Person(firstName, lastName, age) {
  21.             var _self = this;
  22.             if (firstName.length > 20 || firstName.length < 3 ||
  23.                 lastName.length > 20 || lastName.length < 3) {
  24.                 throw new Error('First and last name must be 3 and 20 characters!')
  25.             } else if (!(typeof(firstName) == "string" || typeof(lastName) == "string")) {
  26.                 throw  new Error('First and last name must always be strings!')
  27.             } else if (!(checkForLatinLetters(firstName) || checkForLatinLetters(lastName))) {
  28.                 throw new Error('First and last name must containing only Latin letters!')
  29.             } else if (age > 150 || age < 0) {
  30.                 throw new Error('Age must be between 0 and 150!')
  31.             }
  32.  
  33.             this.firstname = firstName;
  34.             this.lastname = lastName;
  35.             this.age = age;
  36.         }
  37.  
  38.         Person.prototype = {
  39.             get fullname() {
  40.                 return this.firstname + ' ' + this.lastname;
  41.             },
  42.             set fullname(name) {
  43.                 var words = name.split(' ');
  44.                 this.firstname = words[0] || '';
  45.                 this.lastname = words[1] || '';
  46.             },
  47.             introduce: function () {
  48.                 return 'Hello! My name is ' + this.fullname + ' and I am ' + this.age + '-years-old'
  49.             }
  50.         }
  51.  
  52.         function checkForLatinLetters(string) {
  53.             var onlyLetters = /^[a-zA-Z]*$/.test(string);
  54.  
  55.             return onlyLetters;
  56.         }
  57.  
  58.         return Person;
  59.     }());
  60.     return Person;
  61. }
  62. module.exports = solve;
  63.  
  64. // alternative
  65. /* Task Description */
  66. /*
  67.  Create a function constructor for Person. Each Person must have:
  68.  *  properties `firstname`, `lastname` and `age`
  69.  *  firstname and lastname must always be strings between 3 and 20 characters, containing only Latin letters
  70.  *  age must always be a number in the range 0 150
  71.  *  the setter of age can receive a convertible-to-number value
  72.  *  if any of the above is not met, throw Error
  73.  *  property `fullname`
  74.  *  the getter returns a string in the format 'FIRST_NAME LAST_NAME'
  75.  *  the setter receives a string is the format 'FIRST_NAME LAST_NAME'
  76.  *  it must parse it and set `firstname` and `lastname`
  77.  *  method `introduce()` that returns a string in the format 'Hello! My name is FULL_NAME and I am AGE-years-old'
  78.  *  all methods and properties must be attached to the prototype of the Person
  79.  *  all methods and property setters must return this, if they are not supposed to return other value
  80.  *  enables method-chaining
  81.  */
  82. function solve() {
  83.     var Person = (function () {
  84.         function Person(firstName, lastName, age) {
  85.             var _self = this;
  86.             if (firstName.length > 20 || firstName.length < 3 ||
  87.                 lastName.length > 20 || lastName.length < 3) {
  88.                 throw new Error('First and last name must be 3 and 20 characters!')
  89.             } else if (!(typeof(firstName) == "string" || typeof(lastName) == "string")) {
  90.                 throw  new Error('First and last name must always be strings!')
  91.             } else if (!(checkForLatinLetters(firstName) || checkForLatinLetters(lastName))) {
  92.                 throw new Error('First and last name must containing only Latin letters!')
  93.             } else if (age > 150 || age < 0) {
  94.                 throw new Error('Age must be between 0 and 150!')
  95.             }
  96.  
  97.             this.firstname = firstName;
  98.             this.lastname = lastName;
  99.             this.age = age;
  100.  
  101.             Object.defineProperty(this, 'fullname', {
  102.                 get: function() {
  103.                     return this.firstname + ' ' + this.lastname;
  104.                 },
  105.                 set: function(name) {
  106.                     var words = name.split(' ');
  107.                     this.firstname = words[0] || '';
  108.                     this.lastname = words[1] || '';
  109.                 }
  110.             });
  111.         }
  112.        
  113.         Person.prototype.introduce = function(){
  114.             return 'Hello! My name is ' + this.fullname + ' and I am ' + this.age + '-years-old';
  115.         }
  116.  
  117.         function checkForLatinLetters(string) {
  118.             var onlyLetters = /^[a-zA-Z]*$/.test(string);
  119.  
  120.             return onlyLetters;
  121.         }
  122.  
  123.         return Person;
  124.     }());
  125.    
  126.     return Person;
  127. }
  128.  
  129. module.exports = solve;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement