georgiev955

01. Password Reset

Jul 21st, 2023
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function passwordReset(input) {
  2.     let text = input.shift();
  3.     input.pop();
  4.  
  5.     for (let line of input) {
  6.         let [action, ...info] = line.split(' ');
  7.         switch (action) {
  8.             case 'TakeOdd':
  9.                 let newPassword = '';
  10.                 let oldPasswordLength = text.length;
  11.                 for (let i = 0; i < oldPasswordLength; i++) {
  12.                     if (i % 2 !== 0) {
  13.                         newPassword += text[i];
  14.                     }
  15.                 }
  16.                 text = newPassword;
  17.                 console.log(text);
  18.                 break;
  19.             case 'Cut':
  20.                 let [index, length] = info.map(Number);
  21.                 let substring = text.slice(index, index + length);
  22.                 text = text.replace(substring, '');
  23.                 console.log(text);
  24.                 break;
  25.             case 'Substitute':
  26.                 let [substr, substitude] = info;
  27.                 if (text.includes(substr)) {
  28.                     while (text.includes(substr)) {
  29.                         text = text.replace(substr, substitude);
  30.                     }
  31.                     console.log(text);
  32.                 } else {
  33.                     console.log("Nothing to replace!");
  34.                 }
  35.                 break;
  36.         }
  37.     }
  38.     console.log(`Your password is: ${text}`)
  39. }
Advertisement
Add Comment
Please, Sign In to add comment