jPaq Example - Reducing to the Longest Word

Enter as many words as you want in the textbox below and then press the "Get Longest Word Length" button to see how many characters the longest word has:


Get Longest Word Length

Basically, all I am doing is getting an array of all of the words in the textbox and then returning the longest one by iterating through each word with JavaScript's newer reduce() function.  If you really want to test it out, try using something long like an RFC.  Anyway, here is the code to do it:

The regular expression used to capture the words also allows underscores in words.  If you want to use a regular expression that will not allow underscores in the word, you can use this:  /[A-Za-z0-9]+/g.  If you want to allow underscores, but don't want to allow a word to only be underscores, use this: /\w*[A-Za-z0-9]\w*/g.

Applicable Parts

Related Examples

  1. Cartesian Product
    A few weeks ago, I came across a question about finding the cartesian product of two or more arrays in JavaScript.  The following is…
  2. Flatten Arrays
    One of the nice functions that is provided in Prototype is Array.prototype.flatten().  It gives you the ability to turn arrays such as…