Guest User

secretChat

a guest
Jul 8th, 2020
357
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input) {
  2.  
  3.     let chat = input.shift()
  4.     let token = input.shift()
  5.  
  6.     while(token !== `Reveal`) {
  7.  
  8.         let [comand,checkOne,checkTwo] = token.split(`:|:`)
  9.  
  10.         if(comand === `InsertSpace`) {
  11.             chat = space(chat,checkOne)
  12.         } else if(comand === `Reverse`) {
  13.             chat = reversee(chat,checkOne)
  14.  
  15.         } else if(comand === `ChangeAll`) {
  16.           chat =  change(chat,checkOne,checkTwo)
  17.         }
  18.  
  19.        
  20.          token = input.shift()
  21.     }
  22.  
  23.     console.log(`You have a new text message: ${chat}`)
  24.  
  25.     function space(array,index) {
  26.     let result = ``
  27.     let firstPart = ``
  28.     firstPart = array.slice(0,index)
  29.     let secondPart = ``
  30.     secondPart = array.slice(index)
  31.     result = result.concat(firstPart,` `,secondPart)
  32.     array =  result
  33.     console.log(array)
  34.    
  35.        
  36.     return array
  37.     }
  38.  
  39.     function reversee(array,substring) {
  40.  
  41.        
  42.         let result = ``
  43.         let finalRes = ``
  44.         let ind =   array.indexOf(substring)
  45.  
  46.         if(ind > 0) {
  47.         let checkInd = array.slice(ind)
  48.         let revCheck = checkInd.split(``).reverse().join(``)
  49.       result =  array.substring(0,ind)
  50.       let finalRes = ``
  51.     finalRes = finalRes.concat(result,revCheck)
  52.     array = finalRes
  53.     console.log(array)
  54.         }
  55.         else {
  56.             console.log(`error`)
  57.            
  58.         }
  59.        
  60.         return array
  61.     }
  62.  
  63.     function change(array,substring,replacement) {
  64.  
  65.         let result = ``
  66.        
  67.         for(let i=0;i<array.length;i++) {
  68.             if(array[i] == substring) {
  69.              
  70.              result += replacement
  71.             } else {
  72.                 result += array[i]
  73.             }
  74.            
  75.         }
  76.         console.log(result)
  77.         return result
  78.        
  79.     }
  80.  
  81.  
  82. }
Add Comment
Please, Sign In to add comment