Advertisement
tetris555

reset-p

Nov 30th, 2022
796
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  function solve(arr) {
  2.     let text = arr.shift();
  3.     arr.pop();
  4.  
  5.     for (const line of arr) {
  6.         const [cmd, ...rest] = line.split(' ');
  7.         switch (cmd) {
  8.             case 'TakeOdd': {
  9.                 text = [...text].map((ch, idx) => {
  10.                     if (idx % 2 == 1) {
  11.                         return ch;
  12.                     }
  13.                 }).join('');
  14.                 console.log(text);
  15.                 break;
  16.             }
  17.             case 'Cut': {
  18.                 const [idx, len] = rest.map(Number);
  19.                 text = text.slice(0, idx) + text.slice(idx + len);
  20.                 console.log(text);
  21.                 break;
  22.             }
  23.             case 'Substitute': {
  24.                 const [sub, repl] = rest;
  25.                 if (text.includes(sub)) {
  26.                     text = text.replace(new RegExp(`${sub}`, 'g'), () => repl);
  27.                     console.log(text);
  28.                 } else {
  29.                     console.log('Nothing to replace!');
  30.                 }
  31.                 break;
  32.             }
  33.         }
  34.     }
  35.     console.log(`Your password is: ${text}`);
  36.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement