Guest User

Untitled

a guest
Apr 10th, 2020
366
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input){
  2.     let regexp = /([@#])(?<word1>[A-Za-z]{3,})\1{2}(?<word2>[A-Za-z]{3,})\1/g;
  3.     input[0] = input[0].split(' ').join('');
  4.     let array = [...input[0].matchAll(regexp)];
  5.     let validPairs = [];
  6.  
  7.     for(i = 0; i < array.length ; i++){
  8.         let firstWord = array[i].groups.word1;
  9.         let secondWord = array[i].groups.word2;
  10.         validPairs.push(firstWord);
  11.         validPairs.push(secondWord);
  12.     }
  13.  
  14.     if(validPairs.length === 0) {
  15.         console.log(`No word pairs found!`)
  16.     } else{
  17.         let result = Math.round((validPairs.length-1)/2);
  18.         console.log(`${result} word pairs found!`)
  19.     }
  20.  
  21.     let mirrorWords = [];
  22.     for(i = 0; i < array.length ; i++){
  23.         let firstWord = array[i].groups.word1;
  24.         let secondWord = array[i].groups.word2;
  25.  
  26.         if(firstWord === secondWord.split('').reverse().join('')){
  27.             mirrorWords.push(firstWord);
  28.             mirrorWords.push(secondWord);
  29.         }
  30.     }
  31.  
  32.     if(mirrorWords.length > 0){
  33.         console.log(`The mirror words are:`);
  34.         let output = [];
  35.         let str = '';
  36.         for(i = 0; i <= ((mirrorWords.length/2 + 1)); i=i+2){
  37.         str = `${mirrorWords[i]} <=> ${mirrorWords[i+1]}`;
  38.         output.push(str);
  39.         }
  40.         console.log(output.join(', '))
  41.     }
  42.     else{
  43.         console.log(`No mirror words!`)
  44.     }
  45. }
RAW Paste Data