最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Acronym generator in javascript. It only grabs the first letter of the first word, but the not the other words - Stack Overflow

programmeradmin4浏览0评论

Am I missing something in my code? It seems to only grab the first letter, and the while loop, doesn't go onto the next word. So what could I be missing?

function acr(s){
    var words, acronym, nextWord;

    words = s.split();
    acronym= "";
    index = 0
    while (index<words.length) {
            nextWord = words[index];
            acronym = acronym + nextWord.charAt(0);
            index = index + 1 ;
    }
    return acronym
}

Am I missing something in my code? It seems to only grab the first letter, and the while loop, doesn't go onto the next word. So what could I be missing?

function acr(s){
    var words, acronym, nextWord;

    words = s.split();
    acronym= "";
    index = 0
    while (index<words.length) {
            nextWord = words[index];
            acronym = acronym + nextWord.charAt(0);
            index = index + 1 ;
    }
    return acronym
}
Share Improve this question edited May 26, 2011 at 21:23 maerics 157k47 gold badges277 silver badges299 bronze badges asked May 26, 2011 at 21:19 Random DadRandom Dad 511 silver badge2 bronze badges 2
  • Unrelated to your question, but just an aside: be sure to always declare all variables with var or they'll have global scope. Your index variable will be global and thus potentially overwrite an existing global - I'm assuming you don't really mean to do that. (Also, what have you got against for loops?) – nnnnnn Commented May 26, 2011 at 23:58
  • Out of curiosity, was your problem solved? If so please consider asking whichever answer helped you most, and up-voting those you found useful. Otherwise please consider editing your question in order that we can help you further. :) – David Thomas Commented Jun 2, 2011 at 21:44
Add a ment  | 

4 Answers 4

Reset to default 6

If you only care about IE9+ then the answer can be made shorter:

function acronym(text) {
  return text
    .split(/\s/)
    .reduce(function(accumulator, word) {
      return accumulator + word.charAt(0);
    }, '');
}

console.log(acronym('three letter acronym'));

If you can use arrow functions then it can be made shorter still:

function acronym(text) {
  return text
    .split(/\s/)
    .reduce((accumulator, word) => accumulator + word.charAt(0), '');
}

console.log(acronym('three letter acronym'));

Add the separator to the split:

function acr(s){
    var words, acronym, nextWord;

    words = s.split(' ');
    acronym= "";
    index = 0
    while (index<words.length) {
            nextWord = words[index];
            acronym = acronym + nextWord.charAt(0);
            index = index + 1 ;
    }
    return acronym
}

JS Fiddle demo;

Revised the above to make it a little more demonstrative, and also interactive: JS Fiddle demo.


Edited to add references and explanation:

Because no separator was supplied the string remains un-split; therefore the while was operating correctly (as words.length is equal to 1), and so returns only the first letter of the string:

[Separator] specifies the character to use for separating the string. The separator is treated as a string or a regular expression. If separator is omitted, the array returned contains one element consisting of the entire string.

Reference:

  • split(), at MDC Docs

You forgot to split on whitespace:

words = s.split(/\s/);

You can have this in even lesser code. Try this

s.match(/\b(\w)/g).join("").toUpperCase()

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论