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

regex - How do I count letters in JavaScript? - Stack Overflow

programmeradmin1浏览0评论

I have written this function (which is not working) which is supposed to count the letters in a global variable (paraText), and then insert it to count. How do I solve this?

This is a school project so I have to follow sertain rules. I have tried almost all answers but I can't get it to work :( maybe if you look at all the code you can see what I'm doing wrong.

        "use strict";

    var paraText = "";
    var antalParagrafer = 0;

    function addLetter(c) {
        if(!paraText) {
            addParagraph();
        }
        else { //add c to saved textnode
            var tNod = document.createTextNode(c);
            paraText.appendChild(tNod);
    }
    }
        //function is called when enter is pressed
    function addParagraph() {
 /*create a new paragraph with related textnode
    textnode is saved to the global textnodevariable
    add paragraph to the div with id "output"
    you also need to mark the paragraph with the class even/odd
    depending on the class of the previous paragraph*/
        var div = document.getElementById("output");
        var nyParagraf = document.createElement("p");
        div.appendChild(nyParagraf);
        antalParagrafer += 1;
        nyParagraf.className = (antalParagrafer % 2 === 0 ? 'even' : 'odd');
        paraText = nyParagraf;
    }
    //function is called when count letters is pressed
           function countLetters() {
      var count=0;
      for(var i = 0; i < paraText.length; i++) {
        var c = paraText.charAt(i);
        if (c >= 'a' && c <= 'z') count++;
      }
      return count;
    }

I have written this function (which is not working) which is supposed to count the letters in a global variable (paraText), and then insert it to count. How do I solve this?

This is a school project so I have to follow sertain rules. I have tried almost all answers but I can't get it to work :( maybe if you look at all the code you can see what I'm doing wrong.

        "use strict";

    var paraText = "";
    var antalParagrafer = 0;

    function addLetter(c) {
        if(!paraText) {
            addParagraph();
        }
        else { //add c to saved textnode
            var tNod = document.createTextNode(c);
            paraText.appendChild(tNod);
    }
    }
        //function is called when enter is pressed
    function addParagraph() {
 /*create a new paragraph with related textnode
    textnode is saved to the global textnodevariable
    add paragraph to the div with id "output"
    you also need to mark the paragraph with the class even/odd
    depending on the class of the previous paragraph*/
        var div = document.getElementById("output");
        var nyParagraf = document.createElement("p");
        div.appendChild(nyParagraf);
        antalParagrafer += 1;
        nyParagraf.className = (antalParagrafer % 2 === 0 ? 'even' : 'odd');
        paraText = nyParagraf;
    }
    //function is called when count letters is pressed
           function countLetters() {
      var count=0;
      for(var i = 0; i < paraText.length; i++) {
        var c = paraText.charAt(i);
        if (c >= 'a' && c <= 'z') count++;
      }
      return count;
    }
Share Improve this question edited Aug 12, 2012 at 13:21 Eric asked Aug 11, 2012 at 14:19 EricEric 213 bronze badges 1
  • 3 Off-topic suggestion: Instead of making this function dependent on a global variable, add a parameter to its definition (e.g. function countLetters(text)) and then count the letters contained in that argument (i.e., in text instead of paraText). This costs very little, but makes your function more versatile. – stakx - no longer contributing Commented Aug 11, 2012 at 14:25
Add a ment  | 

3 Answers 3

Reset to default 9

I'd just strip out the non-letters and then use the length of what's left:

var count = paraText.replace(/[^a-zA-Z]/g, '').length;

Your function works otherwise fine (although it's perhaps not as elegant as it could be), but count = count ++ is wrong; either use

count++;

or

count = count + 1;

The statement count = count++ doesn't increase the counter, because the value of count++ is what's in the variable before it is increased, so you increse the variable, then assign back the value that was before.

Using a simple parison gives better performance than using a regular expression for each character in the string:

function countLetters() {
  var count=0;
  for(var i = 0; i < paraText.length; i++) {
    var c = paraText.charAt(i);
    if (c >= 'a' && c <= 'z') count++;
  }
  return count;
}
发布评论

评论列表(0)

  1. 暂无评论