Timkor

maxWord

Oct 5th, 2021
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function isLetter(s) {
  2.     if ((s.charCodeAt(0) >= 65 && s.charCodeAt(0) <= 90) ||
  3.         (s.charCodeAt(0) >= 97 && s.charCodeAt(0) <= 122)) {
  4.         return true;
  5.     } else
  6.         return false;
  7. }
  8.  
  9. let line = '  rrqwe     wett gggggg ';
  10. //let wordLength = 0;
  11. //let maxWordLength = 0;
  12. let word = "";
  13. let maxWord = "";
  14. for (let i = 0; i < line.length; i++) {
  15.  
  16.     let isCurrentSymbolLetter = isLetter(line[i]);
  17.  
  18.     if (isCurrentSymbolLetter) {
  19.         //wordLength++;
  20.         word += line[i];
  21.     }
  22.  
  23.     let isCurrentSymbolLast = (line.length - 1 == i);
  24.  
  25.     if (!isCurrentSymbolLetter || isCurrentSymbolLast) {
  26.         if (maxWord.length < word.length) {
  27.             //maxWordLength = wordLength;
  28.             maxWord = word
  29.         }
  30.         //wordLength = 0;
  31.         word = "";
  32.     }
  33. }
  34. console.log(maxWord);
Add Comment
Please, Sign In to add comment