Advertisement
Guest User

Untitled

a guest
Aug 8th, 2021
356
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function mirrorWords(input) {
  2.     let text = input.shift();
  3.     let pattern = /([#@])(?<firstWord>[A-Za-z]{3,})\1{2}(?<secondWord>[A-Za-z]{3,})\1/g; //changed
  4.  
  5.     let mirrorWords = [];
  6.     let wordPairsCount = 0;
  7.     let match = pattern.exec(text);
  8.     while (match) {
  9.         wordPairsCount++;
  10.  
  11.         //console.log(match.first.groups);
  12.         //let firstWord = match.groups.firstWord;
  13.         //let secondWord = match.groups.secondWord;
  14.         let { firstWord, secondWord } = match.groups;
  15.  
  16.         let reversedFirstWord = firstWord.split("").reverse().join(""); //changed
  17.  
  18.         //if (reversedFirstWord === secondWord) {
  19.         //     mirrorWords.push({firstWord: firstWord, secondWord: secondWord});
  20.         //}
  21.  
  22.         if (reversedFirstWord === secondWord) {
  23.             mirrorWords.push(`${firstWord} <=> ${secondWord}`);
  24.         }
  25.  
  26.         match = pattern.exec(text);
  27.  
  28.     }
  29.     if (wordPairsCount < 1) {
  30.         console.log(`No word pairs found!`);
  31.     } else {
  32.         console.log(`${wordPairsCount} word pairs found!`); //changed
  33.     }
  34.     if (mirrorWords.length < 1) {
  35.         console.log(`No mirror words!`);
  36.  
  37.     } else {
  38.         console.log(`The mirror words are:`);
  39.         console.log(mirrorWords.join(", ")); //changed
  40.     }
  41.  
  42.     //for (let pair of mirrorWords) {
  43.     //  console.log(pair.firstWord);
  44.     //}
  45.     //console.log(mirrorWords).join(", ");
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement