Advertisement
Guest User

Untitled

a guest
Apr 4th, 2020
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input) {
  2.     let string = input.shift();
  3.  
  4.     let commands = input.shift();
  5.     let newPassword = ``
  6.  
  7.     while (commands !== `Done`) {
  8.         let tokens = commands.split(` `);
  9.         let command = tokens[0];
  10.         let element1 = tokens[1];
  11.         let element2 = tokens[2];
  12.  
  13.         if (command == `TakeOdd`) {
  14.             for (let i = 0; i < string.length; i++) {
  15.                 if (i % 2 !== 0) {
  16.                 newPassword = newPassword.concat(string[i]);
  17.                 }
  18.             }
  19.             console.log(newPassword);
  20.         } else if (command == `Cut`) {
  21.             let index = +element1;
  22.             let length = +element2;
  23.             let cutString = newPassword.substr(index,length);
  24.  
  25.             newPassword = newPassword.replace(cutString, '');
  26.             console.log(newPassword);
  27.         } else if (command == `Substitute`) {
  28.             if (newPassword.includes(element1)) {
  29.                 while(newPassword.includes(element1)) {
  30.                     newPassword = newPassword.replace(element1,element2);
  31.                 }
  32.                 console.log(newPassword);
  33.                
  34.             } else {
  35.                 console.log(`Nothing to replace!`);
  36.                
  37.             }
  38.         }
  39.         commands = input.shift()
  40.     }
  41.     console.log(`Your password is: ${newPassword}`);
  42.    
  43. }
  44. solve([
  45.     'Siiceercaroetavm!:?:ahsott.:i:nstupmomceqr ',
  46.     'TakeOdd',
  47.     'Cut 15 3',
  48.     'Substitute :: -',
  49.     'Substitute | ^',
  50.     'Done'
  51. ])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement