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>
  <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>


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]

console.log find_longest_word('Web Development Tutorial')

Sample Output:


Live Demo :

See the Pen coffeescript-exercise-13 by w3resource (@w3resource) on CodePen.

Improve this sample solution and post your code through Disqus.

Follow us on Facebook and Twitter for latest update.