Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function mirror(input) {
- let pattern = /(@|#)(?<first>[A-Za-z]{3,})\1\1(?<second>[A-Za-z]{3,})\1/gm;
- let matches = [...input.matchAll(pattern)]
- let mirrors = [];
- for (const match of matches) {
- let fistWord = match[2];
- let secondWord = match[3];
- let reversed = secondWord.split('').reverse().join('');
- if (fistWord === reversed) {
- mirrors.push(fistWord + ' <=> ' + secondWord);
- }
- }
- if (matches.length <= 0) {
- console.log('No word pairs found!')
- console.log('No mirror words!')
- } else {
- console.log(`${matches.length} word pairs found!`);
- if (mirrors.length <= 0) {
- console.log(`No mirror words!`);
- } else {
- console.log(`The mirror words are:`);
- console.log(mirrors.join(', '));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement