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.
5) Write a JavaScript script that will ask the user to enter in the length of...
5) Write a JavaScript script that will ask the user to enter in the length of the side of a cube. (all the sides of a cube are equal) Based on the value that is entered by the user from the keyboard, calculate each of the following and assign your results to variables. 1. volume of the cube 2. surface area of the cube 3. volume of the largest sphere that could fit in the box. 4. surface area of...
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
Write a program that will take a line of input and go through and print out...
Write a program that will take a line of input and go through and print out that line again with all the uppercase letters swapped with the lowercase letters. JAVA Using printf
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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT