TZinovieva

Emoji Detector JS Fundamentals

Mar 31st, 2023
538
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function emojiDetector(text) {
  2.   let emojiPattern = /(::|\*\*)(?<emoji>[A-Z][a-z]{2,})\1/g;
  3.   let thresholdPattern = /[0-9]/g;
  4.  
  5.   let emoji = emojiPattern.exec(text);
  6.  
  7.   let coolThresholdSum = 1;
  8.   text[0]
  9.     .match(thresholdPattern)
  10.     .map(Number)
  11.     .forEach((el) => (coolThresholdSum *= el));
  12.  
  13.   let countOfAllEmojis = 0;
  14.   let result = [];
  15.  
  16.   while (emoji !== null) {
  17.     countOfAllEmojis++;
  18.  
  19.     let currEmoji = emoji.groups.emoji;
  20.     let coolOnes = emoji[0];
  21.  
  22.     let sum = 0;
  23.     for (const currChar of currEmoji) {
  24.       sum += currChar.charCodeAt();
  25.     }
  26.  
  27.     if (sum >= coolThresholdSum) {
  28.       result.push(coolOnes);
  29.     }
  30.  
  31.     emoji = emojiPattern.exec(text);
  32.   }
  33.  
  34.   console.log(`Cool threshold: ${coolThresholdSum}`);
  35.   console.log(
  36.     `${countOfAllEmojis} emojis found in the text. The cool ones are:`
  37.   );
  38.   for (const emoji of result) {
  39.     console.log(emoji);
  40.   }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment