Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function printResult(sentence) {
- let wordsArray = separateSentence(sentence);
- let shortestWord = getShortestWord(wordsArray);
- let longestWord = getLongestWord(wordsArray);
- console.log(`Longest -> ${longestWord}`);
- console.log(`Shortest -> ${shortestWord}`);
- function separateSentence(sentence) {
- let wordsArray = [];
- let sentenceAsWords = sentence.split(' ');
- for (let i = 0; i < sentenceAsWords.length; i++) {
- let currentWord = sentenceAsWords[i];
- wordsArray.push(currentWord);
- }
- return wordsArray;
- }
- function getShortestWord(wordsArray) {
- let shortestWord;
- for (let i = 0; i < wordsArray.length; i++) {
- let currentWord = wordsArray[i];
- if (i === 0) {
- shortestWord = wordsArray[i];
- }
- if (shortestWord.length > currentWord.length) {
- shortestWord = currentWord;
- }
- }
- return shortestWord;
- }
- function getLongestWord(wordsArray) {
- let longestWord;
- for (let i = 0; i < wordsArray.length; i++) {
- let currentWord = wordsArray[i];
- if(i === 0){
- longestWord = wordsArray[i];
- }
- if (currentWord.length > longestWord.length) {
- longestWord = currentWord;
- }
- }
- return longestWord;
- }
- }
- 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