Advertisement
AhmetUstun

07. Cars (Advanced Functions - Lab)

Jun 11th, 2021
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input) {
  2.  
  3.     let result = {};
  4.     let string = '';
  5.  
  6.     let actionList = {
  7.        
  8.         create: function(key) {
  9.             result[key] = {};
  10.             string = key;
  11.         },
  12.  
  13.         inherit: function(key2, key1) {
  14.             result[key1][key2] = {};
  15.         },
  16.  
  17.         set: function(key1, key2, value) {
  18.             if (result.hasOwnProperty(key1)) {
  19.                 result[key1][key2] = value;
  20.             } else if (result[string].hasOwnProperty(key1)) {
  21.                 result[string][key1][key2] = value;
  22.             }
  23.         },
  24.  
  25.         print: function(key) {
  26.  
  27.             if (result.hasOwnProperty(key)) {
  28.                
  29.                 let line = [];
  30.                
  31.                 Object.entries(result[key])
  32.                     .filter((x) => typeof(x[0]) === 'string' && typeof(x[1]) === 'string')
  33.                     .forEach(entry => line.push(`${entry[0]}:${entry[1]}`));
  34.                
  35.                 console.log(line.join(', '));
  36.  
  37.             } else if (result[string].hasOwnProperty(key)) {
  38.                
  39.                 let line = [];
  40.  
  41.                 Object.entries(result[string][key])
  42.                     .filter((x) => typeof(x[0]) === 'string' && typeof(x[1]) === 'string')
  43.                     .forEach(entry => line.push(`${entry[0]}:${entry[1]}`));
  44.  
  45.                 Object.entries(result[string])
  46.                     .filter((x) => typeof(x[0]) === 'string' && typeof(x[1]) === 'string')
  47.                     .forEach(entry => line.push(`${entry[0]}:${entry[1]}`));
  48.  
  49.                 console.log(line.join(', '));
  50.             }
  51.         }
  52.     }
  53.  
  54.     for (let element of input) {
  55.        
  56.         let array = element.split(' ');
  57.        
  58.         if (array.length === 4 && array[0] === 'create') {
  59.             actionList['inherit'](array[1], array[3]);
  60.  
  61.         } else if (array[0] === 'create') {
  62.             actionList['create'](array[1]);
  63.  
  64.         } else if (array[0] === 'set') {
  65.             actionList['set'](array[1], array[2], array[3]);
  66.  
  67.         } else if (array[0] === 'print') {
  68.             actionList['print'](array[1]);
  69.         }
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement