Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(input) {
- let result = {};
- let string = '';
- let actionList = {
- create: function(key) {
- result[key] = {};
- string = key;
- },
- inherit: function(key2, key1) {
- result[key1][key2] = {};
- },
- set: function(key1, key2, value) {
- if (result.hasOwnProperty(key1)) {
- result[key1][key2] = value;
- } else if (result[string].hasOwnProperty(key1)) {
- result[string][key1][key2] = value;
- }
- },
- print: function(key) {
- if (result.hasOwnProperty(key)) {
- let line = [];
- Object.entries(result[key])
- .filter((x) => typeof(x[0]) === 'string' && typeof(x[1]) === 'string')
- .forEach(entry => line.push(`${entry[0]}:${entry[1]}`));
- console.log(line.join(', '));
- } else if (result[string].hasOwnProperty(key)) {
- let line = [];
- Object.entries(result[string][key])
- .filter((x) => typeof(x[0]) === 'string' && typeof(x[1]) === 'string')
- .forEach(entry => line.push(`${entry[0]}:${entry[1]}`));
- Object.entries(result[string])
- .filter((x) => typeof(x[0]) === 'string' && typeof(x[1]) === 'string')
- .forEach(entry => line.push(`${entry[0]}:${entry[1]}`));
- console.log(line.join(', '));
- }
- }
- }
- for (let element of input) {
- let array = element.split(' ');
- if (array.length === 4 && array[0] === 'create') {
- actionList['inherit'](array[1], array[3]);
- } else if (array[0] === 'create') {
- actionList['create'](array[1]);
- } else if (array[0] === 'set') {
- actionList['set'](array[1], array[2], array[3]);
- } else if (array[0] === 'print') {
- actionList['print'](array[1]);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement