Question

In: Computer Science

Write in javaScript: User input 5 words in one input, and print out the longest word.

Write in javaScript: User input 5 words in one input, and print out the longest word.

Solutions

Expert Solution

Please comment if you have any doubt

Code:

<!DOCTYPE html>
<html>
<head>
   <title></title>
</head>
<body>
   <b>Enter words with space between them</b><br>
   <input type="text" id="words" placeholder="Enter Words Here"><br>
   <input type="button" value="Find Longest Word" onclick="findLongestWord();">
<script type="text/javascript">
   //below function will be called when you click button
   function findLongestWord(){
       //the value that is entered in input field with id words is stored in words
       var words=document.getElementById('words').value;
       //Splitting the words with space
       //split('x') will split the string with x and make it a list
       //change the x if you want to use ',','-' or any other separator
       var list_of_words=words.split(' ');
       //assuming that longest word is a empty string
       var longest_word='';
       //accessinf each word from list_of_words
       for(i=0;i<list_of_words.length;i++){
           //if a word's length is long then previous one
           //then it is made as new longest word
           if(list_of_words[i].length>longest_word.length){
               longest_word=list_of_words[i];
           }
       }
       //if no words are entered
       if(longest_word=='')
           document.write("Enter words");
       //Else if words are entered
       else
           document.write("Longest Word is:"+longest_word);
   }
</script>
</body>
</html>

Screenshot:

Output:


Related Solutions

Write java program that will ask for the user for 2 input lines and print out...
Write java program that will ask for the user for 2 input lines and print out all words that occur 1 or more times on both lines (case sensitive). Write this without arrays and method. Here is a sample run: <Output> Enter two lines to process. The quick brown fox jumps over a lazy dog The fox hound outruns the lazy dog The words that occur on both lines are: The fox lazy dog
Write a Python program that reads a file, input by the user, containing one word/token per...
Write a Python program that reads a file, input by the user, containing one word/token per line with an empty line between sentences. The program prints out the longest word found in the file along with its length.
Write a Python program which prompts the user to input a string. Then, print the string...
Write a Python program which prompts the user to input a string. Then, print the string in reverse to the terminal Sample output Please enter a word: "zeus" The reverse of zeus is suez Hint: There are several ways to accomplish this. Recall that a string is an itterable object and therefore can be used with a for loop
Question 1: 5pts Write a program to receive two integers as user input and then print:...
Question 1: 5pts Write a program to receive two integers as user input and then print: Sum Difference Product Average Maximum of the two Minimum of the two You may use the min and max functions declared in the math class. ********************************************************************************** Question 2: 10pts An online bank wants you to create a program that will show a prospective customer, how the deposit will grow. Your program should read the initial balance and the annual interest rate. Interest rate is...
Question 1: Write a program to receive two integers as user input and then print: Sum...
Question 1: Write a program to receive two integers as user input and then print: Sum Difference Product Average Maximum of the two Minimum of the two You may use the min and max functions declared in the math class. ********************************************************************************** Question 2: An online bank wants you to create a program that will show a prospective customer, how the deposit will grow. Your program should read the initial balance and the annual interest rate. Interest rate is compounded monthly....
Write an assembly program that lets the user to input only the word MASM and displays...
Write an assembly program that lets the user to input only the word MASM and displays invalid input for any other user inputs.
Write a Java program that prompts the user to input a word (String). The program must...
Write a Java program that prompts the user to input a word (String). The program must print the reversed word with all consecutive duplicate characters removed. The program must contain the following classes: - The StackX class (you can use the Java Stack class). - The Reverse class which must contain a private data field called “word” of type string, a constructor, and a void method called revNoDup(). The revNoDup() method must reverse the word and remove the consecutive duplicate...
Write a program that will print out “W” using a character that a user provides. use...
Write a program that will print out “W” using a character that a user provides. use for loop statement. in java please
Write a program that gathers input from the user and writes the information out to a...
Write a program that gathers input from the user and writes the information out to a file (output.txt).   Your main method should gather the input, calculate the average, and write the output. You should have a separate method that writes the output to a file. You can have other methods as well if you choose. However, you MUST have at least one other method in addition to the main method. Inputs: Student Number Name Class name Grades 1-5 (5 individual...
Write a java code that allows the user to input any word/name from the console (10...
Write a java code that allows the user to input any word/name from the console (10 words/names only) and then sorts and alphabetizes them into an array without using predefined methods (.compareTo(), or any sorter functions) and then print out the results. you must have three methods that can: Compare multiple strings Swap elements Sort and alphabetize string ***REMEMBER DO NOT USE ANY PRE-DEFINED METHODS*** Example of how the code should work: Enter names/words (10 only): "james, john, alex, aaron,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT