ErolKZ

Untitled

Sep 5th, 2021
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.93 KB | None | 0 0
  1.  
  2. var Person = function (firstAndLast) {
  3.  
  4. // Only change code below this line
  5. // Complete the method below and implement the others similarly
  6.  
  7.  
  8. let arr = [];
  9.  
  10. let counterLastName = 0;
  11.  
  12. let counterFirstName = 0;
  13.  
  14. let counterFullName = 0;
  15.  
  16.  
  17. this.setFirstName = function (firstName) {
  18.  
  19. counterFirstName++;
  20.  
  21. arr.push(firstName);
  22.  
  23. return firstName;
  24.  
  25. }
  26.  
  27. this.setLastName = function (lastName) {
  28.  
  29. counterLastName++;
  30.  
  31. arr.push(lastName);
  32.  
  33. return lastName;
  34.  
  35. }
  36.  
  37.  
  38. this.setFullName = function (fullName) {
  39.  
  40. counterFullName++;
  41.  
  42. arr = fullName.split(' ');
  43.  
  44.  
  45. console.log(arr);
  46.  
  47. return fullName;
  48.  
  49. }
  50.  
  51.  
  52.  
  53.  
  54.  
  55. this.getFullName = function () {
  56.  
  57.  
  58. if (counterFullName > 0) {
  59.  
  60. return `${arr[0]} ${arr[1]}`;
  61.  
  62. } else if (arr.length === 1 && counterFirstName > 0) {
  63.  
  64. return `${arr[0]} ${this.getLastName()}`;
  65.  
  66. } else if (arr.length === 1 && counterLastName > 0) {
  67.  
  68. return `${this.getFirstName()} ${arr[0]}`;
  69.  
  70. } else if (arr.length === 2) {
  71.  
  72. return `${arr[0]} ${arr[1]}`;
  73.  
  74. } else {
  75.  
  76. return `${this.getFirstName()} ${this.getLastName()}`;
  77.  
  78. }
  79.  
  80. };
  81.  
  82.  
  83.  
  84.  
  85. this.getFirstName = function () {
  86.  
  87. if (counterFullName > 0) {
  88.  
  89. return `${arr[0]}`;
  90.  
  91. } else {
  92.  
  93. return firstAndLast.split(' ')[0];
  94.  
  95. }
  96.  
  97.  
  98.  
  99. }
  100.  
  101.  
  102. this.getLastName = function () {
  103.  
  104. if (counterFullName > 0) {
  105.  
  106. return `${arr[1]}`;
  107.  
  108. } else {
  109.  
  110. return firstAndLast.split(' ')[1];
  111.  
  112. }
  113.  
  114.  
  115.  
  116. }
  117.  
  118.  
  119.  
  120. return;
  121.  
  122. };
  123.  
  124.  
  125.  
  126. var bob = new Person('Bob Ross');
  127.  
  128.  
  129.  
  130. // bob.setFirstName("Haskell");
  131.  
  132. // bob.setLastName("Curry");
  133.  
  134. bob.setFullName("Haskell Curry");
  135.  
  136. console.log(bob.getFullName());
  137.  
  138. console.log(bob.getFirstName());
  139.  
  140. console.log(bob.getLastName());
  141.  
  142. console.log(Object.keys(bob).length);
  143.  
  144. console.log(Object.keys(bob));
  145.  
  146. console.log(bob['setFirstName']);
  147.  
  148.  
Advertisement
Add Comment
Please, Sign In to add comment