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 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 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,...
Objectives: Write a program which reads User Input using Scanner Print formatted output using printf or...
Objectives: Write a program which reads User Input using Scanner Print formatted output using printf or DecimalFormat Practice programming simple mathematical calculations Instructions: Samwise Gamgee has finally decided to submit his expense report for all of his adventures regarding his travels to Mordor. Part of those expenses are his stay at the Prancing Pony Inn located in Bree. You are to write a simple Java program which will generate an Invoice for his stay at the Inn. Your program should...
Write a C++ program that reads integers from standard input until end of file. Print out...
Write a C++ program that reads integers from standard input until end of file. Print out the largest integer that you read in, on a line by itself. Any erroneous input (something that is not an integer) should be detected and ignored. In the case where no integers are provided at all, print NO INTEGERS and stop. The whole program is under 40 lines of code. Just read from cin. Now, you need to detect non integers. In this case,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT