AreWe

Emoji Detector JS

Aug 4th, 2020
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. emojiDetector = ([text = '']) => {
  2.     const threshold = text.match(/[0-9]/g).reduce((a, b) => a * b, 1);
  3.     let [emojis, cool] = [0, []];
  4.     const pattern = /(\*\*|::)(?<emoji>[A-Z]{1}[a-z]{2,})\1/g;
  5.     while ((result = pattern.exec(text))) {
  6.         const emoji = result.groups.emoji;
  7.         const sum = emoji.split('').reduce((sum, char) => sum + char.charCodeAt(), 0);
  8.         emojis++;
  9.         if (sum > threshold) {
  10.             cool.push(result[0]);
  11.         }
  12.     }
  13.  
  14.     console.log(`Cool threshold: ${threshold}`);
  15.     console.log(`${emojis} emojis found in the text. The cool ones are:`);
  16.     console.log(cool.join('\n'));
  17. };
Add Comment
Please, Sign In to add comment