Guest User

secretChat

a guest
Jul 8th, 2020
457
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.         index = Number(index)
  27.    
  28.    let firstPart = array.substring(0,index)
  29.     let secondPart = array.substring(index)
  30.    
  31.     array = firstPart + ` ` + secondPart  
  32.     console.log(array)
  33.    
  34.        
  35.     return array
  36.     }
  37.  
  38.     function reversee(array,substring) {
  39.        
  40.        if(array.includes(substring)) {
  41.         let ind =   array.indexOf(substring)
  42.         let secondPart = array.substring(ind,ind + substring.length).split(``).reverse().join(``)
  43.         let firstPart = array.substring(0,ind)
  44.         let thirthPart = array.substring(ind + substring.length)
  45.         array = firstPart + secondPart + thirthPart
  46.         console.log(array)
  47.        } else {
  48.            console.log(`error`)
  49.        }
  50.         return array
  51.     }
  52.  
  53.     function change(array,substring,replacement) {
  54.  
  55.         while(array.includes(substring)) {
  56.             let ind = array.indexOf(substring)
  57.             let part = array.substring(ind,ind + substring.length)
  58.             array = array.replace(part,replacement)
  59.         }
  60.        
  61.         console.log(array)
  62.        return array
  63.     }
  64.  
  65.  
  66. }
Add Comment
Please, Sign In to add comment