Initial letters of the words in a string

Discussion in 'Javascript' started by Swifty, Sep 8, 2011.

  1. Swifty

    Swifty Guest

    Given a string of words such as "The quick brown fox jumps over the
    lazy dog", how easy would it be to generate a string containing the
    initial letters of the words, in lower case, e.g. 'tqbfjotld'

    As it happens, the string will come from the content (value) of one
    <INPUT TYPE=TEXT> control, and the output will replace the content
    (value) of another INPUT control on a user action, such as a mouse

    I don't have a page to demonstrate how far I've already got, as the
    idea only just occurred to me. I can manage the HTML, and the
    JavaScript to get the value of the first INPUT control, and then set
    the content of the second INPUT control.

    What I'm looking for is the JavaScript equivalent of my initials()
    function that I have in my REXX subroutines library:

    Inits = ''
    Do I = 1 to words(string)
    Inits = Inits || left(word(string,I),1)
    Swifty, Sep 8, 2011
    1. Advertisements

  2. Swifty

    Evertjan. Guest

    Swifty wrote on 08 sep 2011 in comp.lang.javascript:
    It is easy in Javascript.
    We should, however, not do your schoolwork.

    Remember that Javascript has Regular Expressions,
    so amaze your teacher with a fast and compact solution,
    using just on line without loops.
    Evertjan., Sep 8, 2011
    1. Advertisements

  3. It depends on the exact definition of "word" and "letter", but assuming
    very simple definitions (a word is a maximal sequence of non-space
    letters, and any non-space character qualifies as "first letter"), quite

    <!doctype html>
    <title>Extract first letters</title>
    <input id=data size=50 style="width: 100%">
    <input type=button value=Extract onclick=
    "document.getElementById('result').value =
    firstLetters(document.getElementById('data').value) ">
    <input id=result>
    function firstLetters(str) {
    var words = str.split(' ');
    var result = '';
    var i;
    for(i = 0; i < words.length; i++) {
    result += words.charAt(0).toLowerCase();
    return result;

    I have assumed that the first character needs to be converted to
    lowercase. This part too is easy _unless_ you need to consider the case
    where the text is in Turkish, Azeri, or another language that
    distinguishes between normal i and dotless i.

    If "letter" means literally "letter" as an alphabetic character, then
    things get rather difficult. There is no direct way in JavaScript to
    decide whether a given character is alphabetic. It would still be
    doable, but laborious, and you would need to fix the concept of "letter"
    _somehow_ (e.g., "a character defined as a letter in Unicode version 6.0").

    Then again, if "letter" means just any simple Latin letter from "a" to
    "z", then things are fairly simple.
    It was _ages_ ago when I used REXX, and not much, but I guess that the
    code works with rather simple concepts of "word" and "letter". But
    "word" might differ from the simplest definition - for example, would
    the "words" in "Hello, world!" be "Hello," and "world!" or "Hello" and
    Jukka K. Korpela, Sep 8, 2011
  4. Very easy. Assuming that "word" refers to the ECMAScript meaning:

    "The quick brown fox jumps over the lazy dog"

    Thomas 'PointedEars' Lahn, Sep 8, 2011
  5. Swifty

    Evertjan. Guest

    Thomas 'PointedEars' Lahn wrote on 08 sep 2011 in comp.lang.javascript:
    s = s.replace(/\s*(\S)\S*(\s+|$)/g,'$1').toLowerCase()
    Evertjan., Sep 8, 2011
  6. Swifty

    RobG Guest

    why not:


    Homework complete - until the asked "how does it work?"
    RobG, Sep 9, 2011
  7. Swifty

    Swifty Guest

    Yes, blank delimited words, probably nothing but a-z and maybe 0-9.

    So far, the webpage concerned is used only in the UK and South Africa,
    but could spread anywhere where my corporate employer does business.
    I have safety net, however; I'm retiring in under two years, so my
    "stuff" is unlikely to spread much further while I'm still supporting
    If they run into problems after I'm gone, I'll recommend that they ask
    you; the hourly rates for consultancy are attractive. :)

    Your example does exactly what I was hoping for, so, sometimes you
    don't have to be careful what you wish for.
    Swifty, Sep 9, 2011
  8. Swifty

    Evertjan. Guest

    RobG wrote on 09 sep 2011 in comp.lang.javascript:
    Not, because starting spaces are not filtered out.
    Indeed, the op will not have learned much.
    Evertjan., Sep 9, 2011
  9. Swifty

    Swifty Guest

    As a matter of principle, I don't put into production any code that I
    couldn't explain to a bright child.

    As for all the "homework" comments: of course it's homework; I work
    from home. Here's the view from my "office" over the top of my

    It sure is nice to have you people helping me with my homework. Just
    don't expect to help me with spending my salary. I've got a wife for
    that :)
    Swifty, Sep 9, 2011
  10. Swifty

    Scott Sauyet Guest

    Thanks for your good humor about this. It does *sound* like a
    homework problem. But your posting equivalent code in Rexx probably
    should have lent you some legitimacy (unless CS programs are now
    teaching Rexx as a prerequisite to JS :) )

    I am amused by those who posted solutions but still thought it to be
    homework, though. Why not post hints and suggestions until the OP
    actually coughs up some code?

    -- Scott
    Scott Sauyet, Sep 9, 2011
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.