-4

I am currently trying to write a ''web application'' that has a simple text area inside, in which I want the letters of the text written to be pointed out.

For example, if I write:

''How old are you? I am 19 years old''

I need a code to tell me how many 'A's and 'Y's and 'D's (and all letters of the alphabet from 0-26) are used in this sentence when I press a button on a HTML/ CSS page.

Could you please tell me what I must write into my .JS file and what I should write into my .HTML file to do this with a click of a button when something is written in the ?

I hope my explanation was detailed enough. Thanks!

Edit (I'm very sorry for the problems I caused) - What I have done so far looks like this:

HTML:

<link rel="stylesheet" type="text/css" href="theme.css">
<meta charset="utf-8"> 

<script src="test.js" type="text/javascript"></script>

<div class='header'>
Al.Fa.Be
</div>

<div class='yaz'>
<textarea></textarea>

</div>

<div class='description'>
<a href='http://www.google.com'>Ara</a>
</div>

<div class='description2'>
<input id="clickMe" type="button" value="Hesapla" onclick="doFunction();" />
</div>

CSS:

body{
background:white;
}

selection{
background:#CCC;
}

#clickMe{
background:#CCC;
border:1px solid #333;

}

.header{
font-size:70px;
font-weight:bold;
font-family:Arial;
color:#333;
margin-left:580px;
padding-top:200px;
}

textarea{
width:1210px;
height:40px;
color:black;
margin-top:20px;
margin-left:100px;
padding-left:10px;
padding-top:10px;
font-size:18px;
font-family:Arial;
}

.description{
background:#f2f2f2;
padding:6px;
width:50px;
text-align:center;
border:1px solid #ddd;
font-family:Arial;
margin-left:620px;
margin-top:20px;
font-size:14px;
}

.description a{
color:#555;
text-decoration:none;
}

.description2{
background:#f2f2f2;
padding:6px;
width:60px;
text-align:center;
border:1px solid #ddd;
font-family:Arial;
margin-left:750px;
margin-top:-30px;
font-size:14px;
}

.description2 a{
color:#555;
text-decoration:none;
}

.yaz{
color:white;
}

Javascript:

// Input name. Count number of alphabets a-z
class program                                                          
{
    public static void main(String[] args) 
    {
        String name = args[0];
        int count[] = new int[29];
        int i,p;
        int n = name.length();
        name = name.toUpperCase();
        char c;
        for (i=0; i<29; i++) 
        {
            count[i] = 0;   
        }
        for (i=0; i<n; i++) 
        {
            c = name.charAt(i);
            p = (int) c;
            count[p-65]++;
        }
        for (i=0; i<29 ; i++) 
        {
            if (count[i] >0) 
            {
                System.out.println((char)(i+65) + " occurs " + count[i] + " times");
            }
        }
    }
}

PS: Please remember that I really suck at this and I need to know how to do this in a specific way.

Doguhanca
  • 11
  • 1
  • 2
  • 1
    Hi and welcome to StackOverflow. You should know that this site is not a place where one places an order and the other members serve them ready-made code; the stated aim is to help fellow programmers solve the problems they encounter. If you have tried something and it doesn't work please show us and ask a specific question; the current form of the question ("tell me what I must write") is off topic. – Jon Nov 09 '13 at 15:45
  • 1
    -1 for multiquestioning. The first part is an okay question but the whole just proves you need more than just an SO answer. – John Dvorak Nov 09 '13 at 15:45
  • As for the first part, just a hint: I'd probably utilise the regex `/[a-z]/gi`. – John Dvorak Nov 09 '13 at 15:48
  • @Doguhanca You should really pick up a book or look at an online Javascript intro. Please see also http://stackoverflow.com/tags/javascript/info, there's a "Learning JavaScript" section. – Olaf Dietsche Nov 09 '13 at 16:08

3 Answers3

1

This is how I would do it.

var count = {};
var msg = "Your message";

for(var i = 0; i < msg.length; i++) {
    var c = msg[i];
    c = c.toLowerCase();
    if(typeof(count[c]) == "undefined") {
        count[c]=1;
    }
    else {
        count[c]++;
    }
}

If you want, you can check for only letters like this:

if('a'.charCodeAt(0) <= c <= 'z'.charCodeAt(0)) 
    //This is a small letter
Alex
  • 467
  • 4
  • 13
0

Check out this article: http://davidwalsh.name/javascript-unique-letters-string. It's just what are you looking for.

Edit: HTML:

<link rel="stylesheet" type="text/css" href="theme.css">
<meta charset="utf-8"> 

<script src="test.js" type="text/javascript"></script>

<textarea rows="5" cols="10" id="str" value="Insert the string" />
<input type="button" value="Submit" id="button" />

JS:

document.getElementById('button').onclick = function() {
/* returns the size/length of an object */
Object.size = function(obj) {
    var size = 0;
    for(key in obj) {
        if(obj.hasOwnProperty(key)) size++;
    }
    return size;
}

//initial vars
var str = document.getElementById('str').val;
var letters = new Object;

//loop, figure it out
for(x = 0, length = str.length; x < length; x++) {
    var l = str.charAt(x)
    letters[l] = (isNaN(letters[l]) ? 1 : letters[l] + 1);
}

//output count!
for(key in letters) {
    console.log(key + ' :: ' + letters[key]);
}
console.log(Object.size(letters));

}

PS: your "javascript" doesn't look like javascript... It looks like java...

aksu
  • 5,221
  • 5
  • 24
  • 39
  • That is indeed what I was looking for. However, how do I combine this with my HTML/ CSS codes that I have written above? I have no idea. I want to click a button and this to happen when something is written in the text area on my page. Can you explain that? That would be awesome! – Doguhanca Nov 09 '13 at 15:55
  • @Doguhanca Okey, there you have. – aksu Nov 09 '13 at 16:06
0

Try this:

var str = "How old are you? I am 19 years old";
str = str.toLowerCase();// search is case sensitive
var counta = str.match(/a/g);
var countd = str.match(/d/g);
var county = str.match(/y/g);
alert("a:"+counta.length+" d:"+countd.length+" y:"+county.length);

Fiddle here:

codingrose
  • 15,563
  • 11
  • 39
  • 58
  • This is pretty close to what I asked for. However: 1. How to do this in a text area? 2. How to do this on a HTML file? I mean I will copy and paste this to a .JS file and what do I have to do on the .HTML file? – Doguhanca Nov 09 '13 at 15:59
  • 1
    @Doguhanca I am not here to write you everything for free.. put some efforts or hire someone I can explain you, ractify your code but NOT write all for you – codingrose Nov 09 '13 at 16:02