georgiev955

Untitled

Jul 23rd, 2023
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function passwordReset(input) {
  2.     let password = input.shift();
  3.  
  4.     let commandParser = {
  5.         'TakeOdd': (password) => {
  6.             return [...password].filter((symbol, index) => {
  7.                 return index % 2 !== 0;
  8.             }).join('');
  9.         },
  10.         'Cut': (password, index, length) => {
  11.             index = Number(index);
  12.             length = Number(length);
  13.  
  14.             let substring = password.substr(index, length);
  15.  
  16.             return password.replace(substring, '');
  17.         },
  18.         'Substitute': (password, substring, substitute) => {
  19.             if (password.includes(substring)) {
  20.                 return password.replace(new RegExp(substring, 'g'), substitute);
  21.             }
  22.             console.log("Nothing to replace!");
  23.             return password;
  24.         }
  25.     };
  26.  
  27.     input.forEach(line => {
  28.         if (line !== 'Done') {
  29.             let [command,...tokens] = line.split(' ');
  30.             let oldPassword = password;
  31.  
  32.             password = commandParser[command](password,...tokens);
  33.  
  34.             if (oldPassword !== password) {
  35.                 console.log(password);
  36.             }
  37.         }
  38.     })
  39.      
  40.     console.log(`Your password is: ${password}`);
  41. }
  42.  
  43.  
  44. ======================================================
  45.  
  46. function passwordReset(input) {
  47.     let text = input.shift();
  48.     input.pop();
  49.  
  50.     for (let line of input) {
  51.         let [action, ...info] = line.split(' ');
  52.         switch (action) {
  53.             case 'TakeOdd':
  54.                 let newPassword = '';
  55.                 let oldPasswordLength = text.length;
  56.                 for (let i = 0; i < oldPasswordLength; i++) {
  57.                     if (i % 2 !== 0) {
  58.                         newPassword += text[i];
  59.                     }
  60.                 }
  61.                 text = newPassword;
  62.                 console.log(text);
  63.                 break;
  64.             case 'Cut':
  65.                 let [index, length] = info.map(Number);
  66.                 let substring = text.slice(index, index + length);
  67.                 text = text.replace(substring, '');
  68.                 console.log(text);
  69.                 break;
  70.             case 'Substitute':
  71.                 let [substr, substitude] = info;
  72.                 if (text.includes(substr)) {
  73.                     while (text.includes(substr)) {
  74.                         text = text.replace(substr, substitude);
  75.                     }
  76.                     console.log(text);
  77.                 } else {
  78.                     console.log("Nothing to replace!");
  79.                 }
  80.                 break;
  81.         }
  82.     }
  83.     console.log(`Your password is: ${text}`)
  84. }
Advertisement
Add Comment
Please, Sign In to add comment