Advertisement
Silviya7

Spellbook Unraveling

Apr 21st, 2024
802
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input){
  2. let message= input.shift();
  3.  
  4. let firstLine=input.shift();
  5. while(firstLine !='End'){
  6.     const[cmd, fromindex, toindex]= firstLine.split('!');
  7.  
  8.     switch(cmd){
  9.  case'RemoveEven':
  10.  
  11.  let newMessage='';
  12.    for (let i = 0; i < message.length; i++) {
  13.      if(i % 2==0){
  14.         newMessage +=message[i];
  15.      }        
  16.    }
  17.      message= newMessage;
  18.      console.log(message);
  19.  break;
  20.  case'TakePart':
  21.  let start=Number(fromindex);
  22.  let end=Number(toindex);
  23.  let newMessage1='';
  24.    newMessage1= message.substring(start,end);
  25.    message=newMessage1;
  26.    console.log(message);
  27.  break;
  28.  case'Reverse':
  29.  let substr=fromindex;
  30.  if(message.includes(substr)){
  31.  
  32.   //cut it out
  33.   message= message.replace(substr,"");
  34.  
  35.  //reverse
  36.  let newcuttedmess='';
  37.  for (let j =substr.length-1; j >=0 ; j--) {
  38.  newcuttedmess +=substr[j];
  39.    
  40.  }
  41.  message +=newcuttedmess;// add to the ened
  42.  console.log(message);
  43.  
  44.  }
  45.  else{
  46.     console.log('Error');
  47.  }
  48.  break;
  49.  
  50.     }
  51.  
  52.     firstLine=input.shift();
  53.  
  54. }
  55.  
  56. console.log(`The concealed spell is: ${message}`);
  57.  
  58.  
  59. }
  60.  
  61. solve(["asAsl2adkda2mdaczsa",
  62. "RemoveEven",
  63. "TakePart!1!9",
  64. "Reverse!maz",
  65. "End"]
  66. );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement