Advertisement
benjaminvr

Untitled

Jun 27th, 2022
501
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  
  3. Intro:
  4.  
  5.     Filtering was completely removed from the project.
  6.     It turned out that this feature was just not needed
  7.     for the end-user and we spent a lot of time just because
  8.     our office manager told us to do so. Next time we should
  9.     instead listen to the product management.
  10.  
  11.     Anyway we have a new plan. CEO's friend Nick told us
  12.     that if we randomly swap user names from time to time
  13.     in the community, it would be very funny and the project
  14.     would definitely succeed!
  15.  
  16. Exercise:
  17.  
  18.     Implement swap which receives 2 persons and returns them in
  19.     the reverse order. The function itself is already
  20.     there, actually. We just need to provide it with proper types.
  21.     Also this function shouldn't necessarily be limited to just
  22.     Person types, lets type it so that it works with any two types
  23.     specified.
  24.  
  25. */
  26.  
  27. interface User {
  28.     name: string;
  29.     age: number;
  30.     occupation: string;
  31. }
  32.  
  33. interface Admin {
  34.     name: string;
  35.     age: number;
  36.     role: string;
  37. }
  38.  
  39. function logUser(user: User) {
  40.     const pos = users.indexOf(user) + 1;
  41.     console.log(` - #${pos} User: ${user.name}, ${user.age}, ${user.occupation}`);
  42. }
  43.  
  44. function logAdmin(admin: Admin) {
  45.     const pos = admins.indexOf(admin) + 1;
  46.     console.log(` - #${pos} Admin: ${admin.name}, ${admin.age}, ${admin.role}`);
  47. }
  48.  
  49. const admins: Admin[] = [
  50.     {
  51.         name: 'Will Bruces',
  52.         age: 30,
  53.         role: 'Overseer'
  54.     },
  55.     {
  56.         name: 'Steve',
  57.         age: 40,
  58.         role: 'Steve'
  59.     }
  60. ];
  61.  
  62. const users: User[] = [
  63.     {
  64.         name: 'Moses',
  65.         age: 70,
  66.         occupation: 'Desert guide'
  67.     },
  68.     {
  69.         name: 'Superman',
  70.         age: 28,
  71.         occupation: 'Ordinary person'
  72.     }
  73. ];
  74.  
  75. export function swap<T1, T2>(v1: T1, v2: T2): [T2, T1] {
  76.     return [v2, v1];
  77. }
  78.  
  79. function test1() {
  80.     console.log('test1:');
  81.     const [secondUser, firstAdmin] = swap(admins[0], users[1]);
  82.     logUser(secondUser);
  83.     logAdmin(firstAdmin);
  84. }
  85.  
  86. function test2() {
  87.     console.log('test2:');
  88.     const [secondAdmin, firstUser] = swap(users[0], admins[1]);
  89.     logAdmin(secondAdmin);
  90.     logUser(firstUser);
  91. }
  92.  
  93. function test3() {
  94.     console.log('test3:');
  95.     const [secondUser, firstUser] = swap(users[0], users[1]);
  96.     logUser(secondUser);
  97.     logUser(firstUser);
  98. }
  99.  
  100. function test4() {
  101.     console.log('test4:');
  102.     const [firstAdmin, secondAdmin] = swap(admins[1], admins[0]);
  103.     logAdmin(firstAdmin);
  104.     logAdmin(secondAdmin);
  105. }
  106.  
  107. function test5() {
  108.     console.log('test5:');
  109.     const [stringValue, numericValue] = swap(123, 'Hello World');
  110.     console.log(` - String: ${stringValue}`);
  111.     console.log(` - Numeric: ${numericValue}`);
  112. }
  113.  
  114. [test1, test2, test3, test4, test5].forEach((test) => test());
  115.  
  116. // In case if you are stuck:
  117. // https://www.typescriptlang.org/docs/handbook/2/everyday-types.html
  118. // https://www.typescriptlang.org/docs/handbook/2/generics.html
  119.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement