elena1234

Closure (JavaScript) with Advanced Functions

Nov 28th, 2021
953
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solution() {
  2.     let text = '';
  3.  
  4.     return {
  5.         append: x => text += x,
  6.         removeStart: n => text = text.slice(n),
  7.         removeEnd: n => text = text.substring(0, text.length - n),
  8.         print: () => console.log(text),
  9.     };
  10. }
  11.  
  12. let secondZeroTest = solution();
  13.  
  14. secondZeroTest.append('123');
  15. secondZeroTest.append('45');
  16. secondZeroTest.removeStart(2);
  17. secondZeroTest.removeEnd(1);
  18. secondZeroTest.print();
  19.  
RAW Paste Data