Advertisement
kstoyanov

01. Password Reset js exam

Aug 8th, 2020
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(args) {
  2.   let inputPassword = args.shift();
  3.   const lines = args.slice(0, args.indexOf('Done'));
  4.   lines.forEach((line) => {
  5.     const [command, value, value2] = line.split(' ');
  6.  
  7.     switch (command) {
  8.       case 'TakeOdd':
  9.         let tempStr = '';
  10.         for (let i = 0; i < inputPassword.length; i++) {
  11.           if (i % 2 !== 0) {
  12.             tempStr += inputPassword[i];
  13.           }
  14.         }
  15.         inputPassword = tempStr;
  16.         console.log(inputPassword);
  17.         break;
  18.  
  19.       case 'Cut':
  20.         const index = Number(value);
  21.         const length = Number(value2);
  22.         const arr = Array.from(inputPassword).splice(index, length).join('');
  23.         inputPassword = inputPassword.replace(arr, '');
  24.         console.log(inputPassword);
  25.         break;
  26.       case 'Substitute':
  27.         const substring = value;
  28.         const substitute = value2;
  29.         if (!inputPassword.includes(substring)) {
  30.           console.log('Nothing to replace!');
  31.         } else {
  32.           while (inputPassword.includes(substring)) {
  33.             inputPassword = inputPassword.replace(substring, substitute);
  34.           }
  35.           console.log(inputPassword);
  36.         }
  37.     }
  38.   });
  39.  
  40.   console.log(`Your password is: ${inputPassword}`);
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement