dilyana2001

mirror words

Aug 12th, 2021 (edited)
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function mirrorWords(arr) {
  2.     let regex = /(#|@)([a-zA-Z]{3,})\1\1([a-zA-Z]{3,})\1/g;
  3.     let exactPairs = [];
  4.     let match = arr.join('').match(regex);
  5.     if (match == null) {
  6.         match = []
  7.     }
  8.     for (let i = 0; i < match.length; i++) {
  9.         let current = match[i].split('');
  10.         let reversed = match[i].split('').reverse();
  11.         if (current.join('') === reversed.join('')) {
  12.             let mirrorWord = match[i].substring(1, match[i].length / 2 - 1);
  13.             exactPairs.push(`${mirrorWord} <=> ${mirrorWord.split('').reverse().join('')}`)
  14.         }
  15.     }
  16.     if (match.length > 0) {
  17.         console.log(`${match.length} word pairs found!`);
  18.     } else {
  19.         console.log(`No word pairs found!`)
  20.     }
  21.     if (exactPairs.length > 0) {
  22.         console.log(`The mirror words are: \n${exactPairs.join(', ')}`);
  23.     } else {
  24.         console.log(`No mirror words!`);
  25.     }
  26. }
  27.  
  28. mirrorWords([
  29.     '#lol#lol# @#God@@doG@# #abC@@Cba# @Xyu@#uyX#'
  30. ])
Add Comment
Please, Sign In to add comment