Advertisement
svetlio_top

Nikulden’s Charity

Mar 31st, 2020
376
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input){
  2.  
  3.     let strForMan = input.shift();
  4.     let command = input.shift();
  5.    
  6.     while(command !== 'Finish'){
  7.  
  8.         if(command.includes("Replace")){                                  
  9.             let arrReplace = command.split(' ');  //Replace a e
  10.             strForMan=strForMan.split(arrReplace[1]).join(arrReplace[2]);
  11.             console.log(strForMan);
  12.         }else if(command.includes("Cut")){    // {startIndex} {endIndex}
  13.              let arrCut = command.split(' ');
  14.              let strsplit = strForMan.split('');
  15.              let startI = Number(arrCut[1]);
  16.              let endI = Number(arrCut[2]);
  17.              if(startI>=0 && endI<strsplit.length){
  18.                 strsplit.splice(startI, endI);
  19.                 strForMan = strsplit.join('');
  20.                 console.log(strForMan);
  21.              }else{
  22.                  console.log('Invalid indexes!');
  23.              }
  24.         }else if(command.includes("Make")){  // {Upper/Lower}  
  25.             if(command.includes("Upper")){
  26.                 strForMan = strForMan.toUpperCase();            
  27.                 console.log(strForMan);
  28.             }else{
  29.                 strForMan = strForMan.toLowerCase();
  30.                 console.log(strForMan);          
  31.             }
  32.         }else if(command.includes("Check")){  // {string}
  33.                 let checkArr = command.split(' ');
  34.                 if(strForMan.includes(checkArr[1])){
  35.                     console.log(`Message contains ${checkArr[1]}`);
  36.                 }else{
  37.                     console.log(`Message doesn't contain ${checkArr[1]}`);
  38.                }
  39.        }else if(command.includes("Sum")){   // {startIndex} {endIndex}
  40.                let sumArr = command.split(' ');
  41.                let startI = Number(sumArr[1]);
  42.                let endI = Number(sumArr[2]);
  43.                let c = 0;
  44.                if(startI>=0 && endI<strForMan.length){
  45.                    let substr = strForMan.substr(startI, endI);
  46.                   //console.log(substr);
  47.                  
  48.                   for (let i = 0; i < substr.length; i++) {  //substr е стринг = LIKE
  49.                       c += substr.charCodeAt(i);
  50.                   }
  51.                    console.log(c);
  52.                    c = 0;
  53.                }else{
  54.                    console.log('Invalid indexes!');
  55.                }    
  56.        }
  57.        command = input.shift();
  58.    }
  59.        
  60. }
  61. solve([
  62.    'ILikeSharan',
  63.    'Replace a e',
  64.    'Make Upper',
  65.    'Check SHEREN',
  66.    'Sum 1 4',
  67.    'Cut 1 4',
  68.    'Finish'
  69.  ]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement