Advertisement
vladovip

JS_Advanced_AdvancedFunctions_LAB07Cars

May 27th, 2023
1,279
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // JS Advanced_ Advanced Functions - LAB - 07. Cars
  2.  
  3. function cars (inputArray) {
  4.     let collectionsObj = {};
  5.     let commandsCollections = {
  6.         create,
  7.         set,
  8.         print,
  9.     }
  10.  
  11.     // 'create c1',
  12.     // 'create c2 inherit c1',
  13.     function create (name, inherits, parentName){
  14.     collectionsObj[name] = inherits ? Object.create(collectionsObj[parentName]) : collectionsObj[name] = {};
  15.     }
  16.     // 'set c1 color red',
  17.     function set (name,key,value){
  18.         collectionsObj[name][key] = value;
  19.     };
  20.  
  21.     //  'print c1',
  22.     function print(name){
  23.         let objectsResult = [];
  24.         for ( let objectAsKey in collectionsObj[name] ){
  25.             objectsResult.push(`${objectAsKey}:${collectionsObj[name][objectAsKey]}`);
  26.         }
  27.         console.log(objectsResult.join(','));
  28.     }
  29.  
  30.     inputArray.forEach(element => {
  31.         let [command, name, key, value] = element.split(" ");
  32.         commandsCollections[command](name,key,value);
  33.     });
  34.  
  35. }
  36.  
  37. cars([
  38.     'create c1',
  39.     'create c2 inherit c1',
  40.     'set c1 color red',
  41.     'set c2 model new',
  42.     'print c1',
  43.     'print c2'
  44.   ]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement