elena1234

Closure (JavaScript) - ObjectComposition

Dec 3rd, 2021
1,062
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input) {
  2.     let result = [];
  3.  
  4.     let listBuilder = {
  5.         add: string => result.push(string),
  6.         remove: string => result = result.filter(x => x !== string),
  7.         print: () => console.log(result.join(",")),
  8.     }
  9.  
  10.     return input.forEach(element => {
  11.         let newElement = element.split(" ");
  12.         let [command, string] = newElement;
  13.         listBuilder[command](string);
  14.     })
  15. }
  16.  
  17. solve(['add pesho', 'add george', 'add peter', 'remove peter', 'print'])
RAW Paste Data