victoriaSD

zadacha 2 - exam prep2

Mar 22nd, 2024
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function emojiDetector([string]) {
  2.   let regexEmoji = /(::|\*\*)([A-Z][a-z]{2,})\1/g;
  3.   let regexNumbers = /[0-9]/g;
  4.   let arrEmoji = [];
  5.   let emojiObject = {};
  6.   let countEmojis = 0;
  7.  
  8.   let matchEmoji = regexEmoji.exec(string);
  9.   let matchNumbers = string.match(regexNumbers).map((n) => Number(n));
  10.   let coolNumber = matchNumbers.reduce((a, b) => a * b);
  11.   console.log(`Cool threshold: ${coolNumber}`);
  12.  
  13.   while (matchEmoji) {
  14.     arrEmoji.push(matchEmoji[0]);
  15.     emojiObject[matchEmoji[0]] = 0;
  16.     matchEmoji = regexEmoji.exec(string);
  17.   }
  18.  
  19.   for (let emoji of arrEmoji) {
  20.     countEmojis++;
  21.     let sum = 0;
  22.     for (let i = 0; i < emoji.length; i++) {
  23.       let letterASCII = emoji[i].charCodeAt();
  24.       sum += letterASCII;
  25.     }
  26.     emojiObject[emoji] = sum;
  27.   }
  28.  
  29.   console.log(`${countEmojis} emojis found in the text. The cool ones are:`);
  30.  
  31.   for (let emoji in emojiObject) {
  32.     if (emojiObject[emoji] >= coolNumber) {
  33.       console.log(emoji);
  34.     }
  35.   }
  36. }
  37.  
  38. emojiDetector([
  39.   "5, 4, 3, 2, 1, go! The 1-th consecutive banana-eating contest has begun! ::Joy:: **Banana** ::Wink:: **Vali** ::valid_emoji::",
  40. ]);
  41.  
Advertisement
Add Comment
Please, Sign In to add comment