Advertisement
Guest User

Untitled

a guest
Jul 27th, 2020
319
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function mirror(input) {
  2.     let pattern = /(@|#)(?<first>[A-Za-z]{3,})\1\1(?<second>[A-Za-z]{3,})\1/gm;
  3.     let matches = [...input.matchAll(pattern)]
  4.     let mirrors = [];
  5.     for (const match of matches) {
  6.         let fistWord = match[2];
  7.         let secondWord = match[3];
  8.         let reversed = secondWord.split('').reverse().join('');
  9.  
  10.         if (fistWord === reversed) {
  11.             mirrors.push(fistWord + ' <=> ' + secondWord);
  12.         }
  13.     }
  14.  
  15.     if (matches.length <= 0) {
  16.         console.log('No word pairs found!')
  17.         console.log('No mirror words!')
  18.     } else {
  19.         console.log(`${matches.length} word pairs found!`);
  20.         if (mirrors.length <= 0) {
  21.             console.log(`No mirror words!`);
  22.         } else {
  23.             console.log(`The mirror words are:`);
  24.             console.log(mirrors.join(', '));
  25.         }
  26.     }
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement