Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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.

  1. 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"]
  2. Do not modify the function just use it in your program.
  3. Inside of the window onload event handler code an event handler for the click event of the analyze button.
  4. 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.
  5. 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.
  6. Sample data:

Input:

The quick brown fox jumps over the lazy dog.

Shortest:

fox, the, dog

Longest:

quick, brown, jumps

  1. 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
  2. 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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Development For Dummies

Authors: Allen G. Taylor

1st Edition

978-0764507526

More Books

Students also viewed these Databases questions