Advertisement
Lulunga

Emoji Sumator Final exam demo alternative (functions)

Jul 28th, 2019
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input) {
  2.     let [text, encryptedEmoji] = input;
  3.     let decryptedEmoji = decryptEmoji();
  4.     let pattern = /(?<=\s):([a-z]{4,}):(?=[\s,.!?])/g;
  5.     let allEmojis = [];
  6.     let totalPower = 0;
  7.  
  8.     function sumAsciiCode(emoji) {
  9.         let sum = emoji.split('').map(e => e.charCodeAt(0)).reduce((a, b) => a + b, 0);
  10.         return sum;
  11.     }
  12.  
  13.     function decryptEmoji() {
  14.         let output = ':'
  15.         let asciiCodes = encryptedEmoji.split(':').map(Number);
  16.         let emoji = asciiCodes.map(e => String.fromCharCode(e)).join('');
  17.         output = output + emoji + ':';
  18.         return output;
  19.     }
  20.  
  21.     let matches = pattern.exec(text);
  22.     while (matches !== null) {
  23.         let [emoji, emojiLetters] = matches;
  24.         allEmojis.push(emoji);
  25.         let currentPower = sumAsciiCode(emojiLetters);
  26.         totalPower += currentPower;
  27.         matches = pattern.exec(text);
  28.     }
  29.     if (allEmojis.includes(decryptedEmoji)) {
  30.         totalPower = totalPower * 2;
  31.     }
  32.     if (allEmojis.length > 0) {
  33.         console.log(`Emojis found: ${allEmojis.join(', ')}`);
  34.     }
  35.     console.log(`Total Emoji Power: ${totalPower}`);
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement