Advertisement
didkoslawow

Untitled

Apr 9th, 2023
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function cars(input) {
  2.     const result = carFactory();
  3.    
  4.     input.forEach(el => {
  5.         if (el.includes('create') && el.includes('inherit')) {
  6.             const [_, name, command, parent] = el.split(' ');
  7.             result[command](name, parent);
  8.         } else if (el.includes('create')) {
  9.             const [command, name] = el.split(' ');
  10.             result[command](name);
  11.         } else if (el.includes('set')) {
  12.             const [command, name, key, value] = el.split(' ');
  13.             result[command](name, key, value);
  14.         } else {
  15.             const [command, name] = el.split(' ');
  16.             result[command](name);
  17.         }
  18.     });
  19.    
  20.    
  21.     function carFactory() {
  22.         const cars = {
  23.             create(name) {
  24.                 cars[name] = {};
  25.             },
  26.             inherit(name, parent) {
  27.                 cars[name] = Object.create(cars[parent]);
  28.             },
  29.             set(name, key, value) {
  30.                 cars[name][key] = value;
  31.             },
  32.             print(name) {
  33.                 const res = [];
  34.  
  35.                 for (const key in cars[name]) {
  36.                     res.push(`${key}:${cars[name][key]}`)
  37.                 }
  38.  
  39.                 console.log(res.join(','));
  40.             }
  41.         }
  42.        
  43.         return cars;
  44.     }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement