TZinovieva

The Most Powerful Word JS

Nov 30th, 2022
65
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 word = input[index];
  4.     index++;
  5.  
  6.     let wordPower = 0;
  7.     let theMostPowerful = "";
  8.     let theBestPower = 0;
  9.     while (word !== "End of words") {
  10.  
  11.         let wordValue = 0;
  12.         let firstLetter = "";
  13.         for (let i = 0; i < word.length; i++) {
  14.            
  15.             let letterValue = word.charCodeAt(i);
  16.             wordValue += letterValue;
  17.         }
  18.             firstLetter = word.charAt(0);
  19.  
  20.             if (firstLetter === "a" || firstLetter === "e" || firstLetter === "i"
  21.             || firstLetter === "o" || firstLetter === "u" || firstLetter === "y"
  22.             || firstLetter === "A" || firstLetter === "E" || firstLetter === "I"
  23.             || firstLetter === "O" || firstLetter === "U" || firstLetter === "Y") {
  24.                 wordPower = Math.floor(wordValue * word.length);
  25.             } else {
  26.                 wordPower = Math.floor(wordValue / word.length);
  27.             }
  28.             if (wordPower >= theBestPower) {
  29.                 theMostPowerful = word;
  30.                 theBestPower = wordPower;
  31.             }
  32.             word = input[index];
  33.             index++;
  34.     }
  35.     console.log(`The most powerful word is ${theMostPowerful} - ${theBestPower}`)
  36. }
Advertisement
Add Comment
Please, Sign In to add comment