How To Find The Longest Word In A String
JavaScript: Find the longest give-and-take inside a string
JavaScript Role: Exercise-6 with Solution
Write a JavaScript function that accepts a string as a parameter and find the longest word inside the string.
Sample Data and output:
Case string: 'Spider web Development Tutorial'
Expected Output: 'Development'
Pictorial Presentation:
Sample Solution: -
HTML Code:
<!DOCTYPE html> <html> <head> <meta charset=utf-viii /> <title>Detect the longest word within a string</championship> </head> <trunk> </body> </html>
JavaScript Code:
function find_longest_word(str) { var array1 = str.friction match(/\west[a-z]{0,}/gi); var outcome = array1[0]; for(var ten = 1 ; 10 < array1.length ; x++) { if(result.length < array1[x].length) { result = array1[x]; } } return result; } console.log(find_longest_word('Spider web Evolution Tutorial'));
Sample Output:
Development
Explanation:
Assume str = '@Web Evolution #Tutorial';
The match() method is used to retrieve the matches when matching a string confronting a regular expression.
Therefore str.match(/\w[a-z]{0,}/gi) will return ["Spider web", "Evolution", "Tutorial"].
For loop checks the length of the array element and compare with previous 1 and finally return the longest string.
The length property represents an unsigned, 32-bit integer that is always numerically greater than the highest alphabetize in the array.
Syntax -> arr.length
Flowchart:
Live Demo:
Come across the Pen JavaScript -Converts the first letter of each word of a cord in upper case-office-ex- 5 by w3resource (@w3resource) on CodePen.
Better this sample solution and mail your code through Disqus
Previous: Write a JavaScript function that accepts a cord equally a parameter and converts the first letter of each word of the cord in upper case.
Adjacent: Write a JavaScript office that accepts a cord every bit a parameter and counts the number of vowels within the string.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
JavaScript: Tips of the Twenty-four hours
Returns the divergence between two arrays, after applying the provided function to each assortment chemical element of both
Example:
const tips_differenceBy = (x, y, fn) => { const south = new Gear up(y.map(fn)); return 10.map(fn).filter(el => !south.has(el)); }; panel.log(tips_differenceBy([iii.1, 1.5], [iii.3, 3.8], Math.floor)); console.log(tips_differenceBy([{ ten: 3 }, { x: 2 }], [{ x: ii }], v => five.x));
Output:
[1] [iii]
- New Content published on w3resource:
- HTML-CSS Applied: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Do, Solution
- Scala Programming Exercises, Do, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Bundle Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework
Source: https://www.w3resource.com/javascript-exercises/javascript-function-exercise-6.php
Posted by: franksconot1980.blogspot.com
0 Response to "How To Find The Longest Word In A String"
Post a Comment