Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13
  1. #1
    New Coder
    Join Date
    Jul 2011
    Posts
    21
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Character counting

    Hi Im new to javascript and need some help with a project I have, the task I have is:

    Create a page that can take a bloke of text and out put the following
    o How many times each letter occurs
    o How many spaces
    o How many words
    o How many times each word occurs
    o A list of things that are not words or special characters

    I think maybe use an array but im not sure, like I said im new to this so not too sure on how to do it. I know it would read into a file say a txt file and sort through it this way, possibly using some kind of loop to read through all of the text before sorting it and I know it might use variables. I know how to kind of work through the task I have, but to actually implement the mechanics of the javascript is something that I then struggle with
    Thanks to all that help.
    Last edited by 9aza; 07-27-2011 at 11:17 AM. Reason: adding more detail

  • #2
    Regular Coder
    Join Date
    Sep 2010
    Location
    Far far away
    Posts
    122
    Thanks
    0
    Thanked 16 Times in 16 Posts
    Look here - https://developer.mozilla.org/en/Jav...s/String/match

    Pay your attention to this excerpt:
    If the regular expression includes the g flag, the method returns an Array containing all matches. If there were no matches, the method returns null.
    The length of this array can answer for all your questions (-:

  • Users who have thanked siberia-man for this post:

    9aza (07-27-2011)

  • #3
    New to the CF scene
    Join Date
    Jul 2011
    Posts
    5
    Thanks
    0
    Thanked 1 Time in 1 Post
    for your purpose you should use the character count program. this may suits you. try this . you can get it from the websites.

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    This sounds like homework, but I'll give you a start:-

    Code:
    <script type = "text/javascript">
    var message = "This is an important message!";
    var chars = [];
    var words = [];
    chars = message.split("");
    words = message.split(" ");
    alert (chars);
    alert (words);
    alert (chars.length);
    alert (words.length);
    </script>
    The German, as we have seen before, so good at bombing his way out of this situation ...... - Wimbledon Tennis Commentator, BBC2

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,857
    Thanks
    78
    Thanked 4,418 Times in 4,383 Posts
    Not sure how siberia-man's answer helps count the occurrences of each letter or the occurrences of each word.

    Or, rather, his answer is just the START of the code needed to count common occurrences.

    Same applies to Philip's answer, of course. It doesn't tell you how many times the letter "a" occurs in the message, just for example.

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Quote Originally Posted by Old Pedant View Post
    of course. It doesn't tell you how many times the letter "a" occurs in the message, just for example.
    Yes, I said "I'll give you a start". As I have the idea that this is a homework question, that is as far as I am willing to go unless/until the OP posts some code of his own. Then people may well improve on or correct it.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #7
    Regular Coder
    Join Date
    Aug 2010
    Posts
    974
    Thanks
    19
    Thanked 212 Times in 210 Posts
    [QUOTE=Old Pedant;1117309]Not sure how siberia-man's answer helps count the occurrences of each letter or the occurrences of each word.

    Or, rather, his answer is just the START of the code needed to count common occurrences.
    QUOTE]

    here is the code siberia-man referred to...
    Code:
    var str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
    var regexp = /[A-E]/gi;
    var matches_array = str.match(regexp);
    document.write(matches_array);
    here is a couple of variations...

    Code:
    var str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
    var regexp = /A/gi;
    var matches_array = str.match(regexp);
    document.write('there are '+matches_array.length+" a's");
    Code:
    var str = "ABCDEFGHIJKL MNOPQRST UVWXYZabcd efghijklmnopqrs tuvwxyz";
    var regexp = / /gi;
    var matches_array = str.match(regexp);
    document.write('there are '+(matches_array.length+1) + " words's");
    Quote Originally Posted by siberia-man View Post

    Pay your attention to this excerpt:

    The length of this array can answer for all your questions (-:
    Last edited by DaveyErwin; 07-27-2011 at 09:19 PM.

  • #8
    Regular Coder
    Join Date
    Sep 2010
    Location
    Far far away
    Posts
    122
    Thanks
    0
    Thanked 16 Times in 16 Posts
    I kept in my mind that this can be homework (in summertime?!). Anyway. I gave him the link to the good article. Careful reading and set of experiments can help to solve this issue. I hope that the topic-starter will be back with examples and new questions.

  • #9
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,857
    Thanks
    78
    Thanked 4,418 Times in 4,383 Posts
    Well, maybe he's in Summer School repeating the course? <grin/>

    Sorry, didn't mean to say either of you had been less than helpful. Just warning 9aza that he still has work to do.

  • #10
    New Coder
    Join Date
    Jul 2011
    Posts
    21
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Hi all sorry for any confusion caused, Im working on a few things some of them quite similar and have gotten it confused with the javascript as it is quite similar to the php I am working on.
    Last edited by 9aza; 08-01-2011 at 05:36 PM. Reason: Posted wrong thing.

  • #11
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    You asked for help with Javascript. If you are wanting php code you should post in the appropriate forum.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #12
    New Coder
    Join Date
    Jul 2011
    Posts
    21
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Philip M View Post
    You asked for help with Javascript. If you are wanting php code you should post in the appropriate forum.
    Yeh sorry about that. Read the post above, have edited it now.

  • #13
    New Coder
    Join Date
    Jul 2011
    Posts
    21
    Thanks
    2
    Thanked 0 Times in 0 Posts
    This is all I have managed to come up with, it only simply counts the spaces so I have managed to get one task down, can someone help as to how I would get to the next steps or what to do from here to reach the next tasks.
    Code:
    <HTML>
    <HEAD>
       <TITLE>Untitled HTML Document</TITLE>
    </HEAD>
    <BODY BGCOLOR="white">
    
    <form name="wordcount">
    <textarea rows="12" name="wordcount2" cols="38" wrap="virtual"></textarea><br>
    <input type="button" value="Calculate values" onClick="countit()"> <input type="text" name="wordcount3" size="20">
    </form>
    
    <script type="text/javascript">
    function countit()
    {
    var formcontent=document.wordcount.wordcount2.value
    formcontent=formcontent.split(" ")
    document.wordcount.wordcount3.value=formcontent.length
    }
    
    </script> 
    </body>
    </html>
    All help appreciated


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •