-Enigmos-

theMostPowerfulWord.js

Nov 15th, 2021 (edited)
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function theMostPowerfulWord(input) {
  2.     let index = 0;
  3.     let command = input[index];
  4.     index++;
  5.     let maxWord = Number.MIN_SAFE_INTEGER;
  6.     let maxWordName = "";
  7.  
  8.     while (command !== "End of words") {
  9.         let asciiSum = 0;
  10.         let firstLetterIsVocal = false;
  11.        
  12.         for (let i = 0; i < command.length; i++) {
  13.             let asciiValue = command.charCodeAt(i);
  14.             let vocals = ["a", "e", "i", "o", "u", "y", "A", "E", "I", "O", "U", "Y"];
  15.  
  16.             for (letter = 0; letter < vocals.length; letter++) {
  17.                 let currentLetter = vocals[letter];
  18.                 let firstLetter = command[0];
  19.                 if (firstLetter === currentLetter) {
  20.                     firstLetterIsVocal = true;
  21.                     break;
  22.                 }
  23.             }
  24.             asciiSum += asciiValue;
  25.         }
  26.  
  27.         if (firstLetterIsVocal) {
  28.             asciiSum *= command.length;
  29.         } else {
  30.             asciiSum = Math.floor (asciiSum / command.length);
  31.         }
  32.  
  33.         if (asciiSum > maxWord) {
  34.             maxWord = asciiSum;
  35.             maxWordName = command;
  36.         }
  37.  
  38.         command = input[index];
  39.         index++;
  40.     }
  41.     console.log(`The most powerful word is ${maxWordName} - ${maxWord}`);
  42. }
  43.  
  44. theMostPowerfulWord(["The", "Most", "Powerful", "Word", "Is", "Experience", "End of words"]);
  45. theMostPowerfulWord(["But", "Some", "People", "Say", "It's", "LOVE", "End of words"]);
Add Comment
Please, Sign In to add comment