Advertisement
bebo231312312321

Untitled

May 1st, 2023
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function cars(input){
  2.  
  3.     const lines = input.slice();
  4.     const data = {};
  5.    
  6.     let result = {
  7.         create:(name, inherit, name2) => (data[name] = inherit ? Object.create(data[name2]) : {} ),
  8.         set : (name,key,value) => (data[name][key]) = value,
  9.         print: (name) => {
  10.             const entry = [];
  11.             for(let key in data[name]){
  12.                 entry.push(`${key}:${data[name][key]}`);
  13.             }
  14.             console.log(entry.join(','));
  15.         }
  16.     }
  17.  
  18.     lines.forEach(line => {
  19.             let [command,name,key,value] = line.split(' ');
  20.            result[command](name,key,value);
  21.     });
  22. }
  23. cars(['create c1',
  24. 'create c2 inherit c1',
  25. 'set c1 color red',
  26. 'set c2 model new',
  27. 'print c1',
  28. 'print c2']);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement