Guest User

Mirror Words

a guest
Apr 14th, 2020
982
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input){
  2.     let text = input[0];
  3.     let reg= /(\@|\#)([A-Za-z]{3,})\1\1([A-Za-z]{3,})\1/gm
  4.     let matches = [...text.matchAll(reg)]
  5.     let mirrorWords=[]
  6.    
  7.     for (const match of matches) {
  8.         let firstWord = match[2]
  9.         let secondWord = match[3]
  10.         let reverseWord = secondWord.split('').reverse().join('')
  11.         if(firstWord===reverseWord){
  12.             mirrorWords.push(firstWord + " <=> " + secondWord)
  13.         }
  14.        
  15.     }
  16.     if(matches.length===0){
  17.         console.log('No word pairs found!')
  18.         console.log('No mirror words!')
  19.     }else{
  20.         console.log(`${matches.length} word pairs found!`)
  21.         if(mirrorWords.length===0){
  22.             console.log('No mirror words!')
  23.         }else{
  24.             console.log('The mirror words are:')
  25.             console.log(mirrorWords.join(', '))
  26.         }
  27.     }
  28. }
RAW Paste Data