Advertisement
Guest User

Untitled

a guest
Apr 4th, 2020
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input) {
  2.     let password = input.shift();
  3.     for (let line of input) {
  4.         if (line === 'Done') {
  5.             console.log(`Your password is: ${password}`)
  6.             break;
  7.         }
  8.  
  9.         let tokens = line.split(' ');
  10.         let command = tokens[0];
  11.         switch (command) {
  12.             case 'TakeOdd':
  13.                 takeodd(password);
  14.                 break;
  15.             case 'Cut':
  16.                 cut(tokens[1],tokens[2])
  17.                 break;
  18.             case 'Substitute':
  19.                 substitute(tokens[1],tokens[2])
  20.                 break;
  21.         }
  22.  
  23.     }
  24.     function takeodd(string){
  25.         let newPassword = "";
  26.         for ( let i= 0; i < string.length; i++){
  27.             if(i%2 != 0){
  28.                 newPassword += string[i]
  29.             }
  30.         }
  31.         password = newPassword;
  32.         console.log(newPassword)
  33.     }
  34.     function cut(startIndex,endIndex) {
  35.         startIndex = Number(startIndex);
  36.         endIndex = Number(endIndex);
  37.         let cutMessage = password.substring(startIndex, startIndex + endIndex);
  38.  
  39.         password = password.replace(cutMessage, '');
  40.         console.log(password);
  41.     }
  42.     function substitute(currentChar,newChar) {
  43.         if(password.includes(currentChar)) {
  44.             while(password.search(currentChar) !== -1) {
  45.                 password = password.replace(currentChar,newChar);
  46.             }
  47.             console.log(password)
  48.  
  49.         } else {
  50.             console.log("Nothing to replace!")
  51.         }
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement