Advertisement
Guest User

Untitled

a guest
Oct 21st, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. function printResult(sentence) {
  2. let wordsArray = separateSentence(sentence);
  3. let shortestWord = getShortestWord(wordsArray);
  4. let longestWord = getLongestWord(wordsArray);
  5. console.log(`Longest -> ${longestWord}`);
  6. console.log(`Shortest -> ${shortestWord}`);
  7.  
  8.  
  9. function separateSentence(sentence) {
  10. let wordsArray = [];
  11. let sentenceAsWords = sentence.split(' ');
  12. for (let i = 0; i < sentenceAsWords.length; i++) {
  13. let currentWord = sentenceAsWords[i];
  14. wordsArray.push(currentWord);
  15. }
  16. return wordsArray;
  17. }
  18.  
  19. function getShortestWord(wordsArray) {
  20. let shortestWord;
  21. for (let i = 0; i < wordsArray.length; i++) {
  22. let currentWord = wordsArray[i];
  23. if (i === 0) {
  24. shortestWord = wordsArray[i];
  25. }
  26. if (shortestWord.length > currentWord.length) {
  27. shortestWord = currentWord;
  28. }
  29. }
  30. return shortestWord;
  31. }
  32.  
  33. function getLongestWord(wordsArray) {
  34. let longestWord;
  35. for (let i = 0; i < wordsArray.length; i++) {
  36. let currentWord = wordsArray[i];
  37. if(i === 0){
  38. longestWord = wordsArray[i];
  39. }
  40. if (currentWord.length > longestWord.length) {
  41. longestWord = currentWord;
  42. }
  43. }
  44. return longestWord;
  45. }
  46. }
  47. printResult('Hello people, are you familiar with the terms of application at the software university?');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement