...

View Full Version : Character counting



9aza
07-27-2011, 11:11 AM
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:confused:
Thanks to all that help.:thumbsup:

siberia-man
07-27-2011, 11:50 AM
Look here - https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/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 (-:

mathewfarrell35
07-27-2011, 07:24 PM
for your purpose you should use the character count program. this may suits you. try this . you can get it from the websites.

Philip M
07-27-2011, 08:51 PM
This sounds like homework, but I'll give you a start:-


<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

Old Pedant
07-27-2011, 08:52 PM
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.

Philip M
07-27-2011, 09:04 PM
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.

DaveyErwin
07-27-2011, 09:14 PM
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...

var str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
var regexp = /[A-E]/gi;
var matches_array = str.match(regexp);
document.write(matches_array);
here is a couple of variations...


var str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
var regexp = /A/gi;
var matches_array = str.match(regexp);
document.write('there are '+matches_array.length+" a's");


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=siberia-man;1117028]

Pay your attention to this excerpt:

The length of this array can answer for all your questions (-:

siberia-man
07-27-2011, 09:22 PM
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.

Old Pedant
07-27-2011, 09:25 PM
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.

9aza
08-01-2011, 05:08 PM
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.

Philip M
08-01-2011, 05:11 PM
You asked for help with Javascript. If you are wanting php code you should post in the appropriate forum.

9aza
08-01-2011, 05:37 PM
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.

9aza
08-11-2011, 02:03 PM
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.


<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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum