Advertisement
ErolKZ

Untitled

Feb 14th, 2022
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | None | 0 0
  1. function getPersons() {
  2.  
  3. let personsArr = [];
  4.  
  5. // Gabriel Peterson 24 g.p@gmail.com
  6.  
  7.  
  8. let persons = [
  9.  
  10. ['Anna', 'Simpson', 22, 'anna@yahoo.com'],
  11.  
  12. ['SoftUni'],
  13.  
  14. ['Stephan', 'Johnson', 25],
  15.  
  16. ['Gabriel', 'Peterson', 24, 'g.p@gmail.com'],
  17.  
  18. ['Erol', 'Mehmed']
  19.  
  20. ];
  21.  
  22. class Person {
  23.  
  24. constructor(firstName, lastName, age, email) {
  25.  
  26. this.firstName = firstName;
  27.  
  28. this.lastName = lastName;
  29.  
  30. this.age = age;
  31.  
  32. this.email = email;
  33.  
  34. };
  35.  
  36. toString() {
  37.  
  38. let person = `${this.firstName} ${this.lastName} (age: ${this.age}, email: ${this.email})`;
  39.  
  40. return person;
  41.  
  42. };
  43.  
  44.  
  45. toString2() {
  46.  
  47. let person = `${this.firstName} ${this.lastName} (age: ${this.age})`;
  48.  
  49. return person;
  50.  
  51. };
  52.  
  53.  
  54. toString3() {
  55.  
  56. let person = `${this.firstName} ${this.lastName}`;
  57.  
  58. return person;
  59.  
  60. };
  61.  
  62.  
  63. toString4() {
  64.  
  65. let person = this.firstName;
  66.  
  67. return person;
  68.  
  69. };
  70.  
  71.  
  72. };
  73.  
  74.  
  75. for (let arr of persons) {
  76.  
  77. let firstName = arr[0];
  78.  
  79. let lastName = arr[1];
  80.  
  81. let age = arr[2];
  82.  
  83. let email = arr[3];
  84.  
  85.  
  86. if (arr.length === 4) {
  87.  
  88. let person = new Person(firstName, lastName, age, email);
  89.  
  90. personsArr.push(person.toString());
  91.  
  92. } else if (arr.length === 3) {
  93.  
  94. let person = new Person(firstName, lastName, age);
  95.  
  96. personsArr.push(person.toString2());
  97.  
  98. } else if (arr.length === 2) {
  99.  
  100. let person = new Person(firstName, lastName);
  101.  
  102. personsArr.push(person.toString3());
  103.  
  104. } else if (arr.length === 1) {
  105.  
  106. let person = new Person(firstName);
  107.  
  108. personsArr.push(person.toString4());
  109.  
  110. }
  111.  
  112. }
  113.  
  114. // console.log(personsArr);
  115.  
  116. return personsArr;
  117.  
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement