w3resource logo


CoffeeScript exercises

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'

Solution :

HTML Code :

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>JS Bin</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

alert find_longest_word('Web Development Tutorial')

Live Demo :

JS Bin on jsbin.com

Post your code through Disqus :



Join our Question Answer community to learn and share your programming knowledge.

Help the community:

Python: Fizzbuzz

C++: Decimal to binary conversion

JavaScript: Need Help in JavaScript

Python: Help me with this program