AreWe

Password Reset alexander iliev

Jul 1st, 2020
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function passWordResset(password) {
  2.  
  3.  
  4.     let elements = password.slice(1);
  5.     let pwd = password.shift();
  6.  
  7.     for (let i = 0; i < elements.length; i++) {
  8.         let currentElement = elements[i].split(' ');
  9.         let command = currentElement[0];
  10.  
  11.         if (command === 'TakeOdd') {
  12.             pwd = pwd.split('').reduce((pwd, char, idx) => {
  13.                 if(idx % 2 === 1) pwd += char;
  14.                 return pwd;
  15.             }, '');
  16.             console.log(pwd);
  17.         } else if (command === 'Cut') {
  18.             let index = Number(currentElement[1]);
  19.             let length = Number(currentElement[2]);
  20.             pwd = pwd.substr(0, index) + pwd.substring(index + length);
  21.             console.log(pwd);
  22.  
  23.         } else if (command === 'Substitute') {
  24.             let currentSymbol = currentElement[1];
  25.             let replace = currentElement[2];
  26.             let isFound = pwd.indexOf(currentSymbol);
  27.             if (isFound !== -1) {
  28.                 pwd = pwd.replace(new RegExp(currentSymbol, 'g'), replace);
  29.                 console.log(pwd);
  30.             } else {
  31.                 console.log(`Nothing to replace!`);
  32.  
  33.             }
  34.         } else if (command === 'Done') {
  35.             console.log(`Your password is: ${pwd}`);
  36.             break;
  37.         }
  38.  
  39.     }
  40. }
Add Comment
Please, Sign In to add comment