Advertisement
kstoyanov

02. Emoji Detector js exam

Aug 12th, 2020
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(args) {
  2.   const text = args.shift();
  3.   const emojisArr = [];
  4.   const coolThresholdNumbers = [];
  5.  
  6.   const patText = /[*]{2}(?<emoji>[A-Z][a-z]{2,})([*]){2}|([:]){2}(?<emoji2>[A-Z][a-z]{2,})[:]{2}/g;
  7.   const patCoolThreshold = /\d/g;
  8.   const takeEmoji = [...text.matchAll(patText)];
  9.   const takeCoolNumbers = [...text.matchAll(patCoolThreshold)];
  10.  
  11.   takeEmoji.forEach((str) => {
  12.     const [textSt] = str;
  13.     emojisArr.push(textSt);
  14.   });
  15.  
  16.   takeCoolNumbers.forEach((el) => {
  17.     const [num] = el;
  18.     coolThresholdNumbers.push(Number(num));
  19.   });
  20.  
  21.   const coolThresholdSum = coolThresholdNumbers.join('')
  22.     .split('')
  23.     .map(Number)
  24.     .reduce((acc, curnV) => acc * curnV);
  25.    
  26.  
  27.   console.log(`Cool threshold: ${coolThresholdSum}`);
  28.   console.log(`${emojisArr.length} emojis found in the text. The cool ones are:`);
  29.   emojisArr.forEach((str) => {
  30.     const takeStr = str.match(/[A-z]+/);
  31.     let theCoolness = 0;
  32.     takeStr[0].split('').forEach((ch) => {
  33.       const takeLeftLetterIndex = text.indexOf(ch);
  34.       const takeLetterCode = text.charCodeAt(takeLeftLetterIndex);
  35.  
  36.       theCoolness += takeLetterCode;
  37.     });
  38.  
  39.     if (theCoolness > coolThresholdSum) {
  40.       console.log(str);
  41.     }
  42.   });
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement