Question
USE JAVASCRIPT This program will allow the user to enter a paragraph of text and then see a listing of the shortest and longest words
USE JAVASCRIPT
This program will allow the user to enter a paragraph of text and then see a listing of the shortest and longest words in their paragraph.
- In the starter file there is a function that takes in a string, lower cases it, removes any characters that aren't letters or spaces and returns an array of words, e.g. if the input was "The quick brown fox jumps over the lazy dog." it would return ["the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"]
- Do not modify the function just use it in your program.
- Inside of the window onload event handler code an event handler for the click event of the analyze button.
- When the analyze button is clicked it should retrieve the user's paragraph of text from the page, turn the string paragraph into an array of words using the supplied function, find the longest and shortest words and output them to the appropriate textareas.
- use a sort by length to find the longest and shortest words: write a compareFunction to use with the array sort method. This method would sort the array by length. With the array sorted you know the first and last elements of the array are the shortest and longest words. You can scan up from the beginning or end of the array until the length of the words change you can see how many more words share the same min or max length.
- Sample data:
Input:
The quick brown fox jumps over the lazy dog.
Shortest:
fox, the, dog
Longest:
quick, brown, jumps
- Also add code that removes duplicate words from the list of the longest and shortest words. For example if the output was Shortest: a, i, i, i Longest: together, sentence, mountain, mountain, together solution would instead show only Shortest: i, a Longest: together, sentence, mountain
- Use code that would alphabetically sort the longest and shortest word arrays. Start new arrays to store the unique words and start each one with the first word for their corresponding array. You can then iterate through the long/short list and add a word to the unique list if it's not equal to the last word in the unique list.
Javascript
"use strict";
var $ = function(id) {
return document.getElementById(id);
};
//just use this function in the onclick method
var strToWords = function(text) {
text = text.toLowerCase();
text = text.replaceAll(/[\- ]/g, " ");
var chars = text.split("");
chars = chars.filter(function(s) {
if(' abcdefghijklmnopqrstuvwxyz'.includes(s)) {
return true;
} else {
return false;
}
})
text = chars.join("");
return text.split(" ").filter(function(e) {
if(e === "" || e === " ") {
return false;
} else {
return true;
}
});
}
window.onload = function() {
};
HTML
Longest Shortest
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started