CoffeeScript function: Find the longest word within a string
CoffeeScript Function : Exercise-13 with Solution
Write a CoffeeScript function that accepts a string as a parameter and find the longest word within the string.
Example string : 'Web Development Tutorial'
Expected Output : 'Development'
HTML Code :
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="//jashkenas.github.io/coffee-script/extras/coffee-script.js"></script>
<title>Find the longest word within a string</title>
</head>
<body>
</body>
</html>
CoffeeScript Code :
find_longest_word = (str) ->
array1 = str.match(/\w[a-z]{0,}/gi)
result = array1[0]
x = 1
while x < array1.length
if result.length < array1[x].length
result = array1[x]
x++
result
console.log find_longest_word('Web Development Tutorial')
Sample Output:
"Development"
Live Demo :
See the Pen coffeescript-exercise-13 by w3resource (@w3resource) on CodePen.
Improve this sample solution and post your code through Disqus.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/coffeescript-exercises/coffeescript-exercise-13.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics