Question: Assuming:
str value = 'This is some text';

I want to count how many 't' occurrences, how to do that?

Answer: 
I think you all make it more complicated than it needs to be. Use regex. This is also case insensitive. If you want case sensitive, remove the i after g.

```
var str = "This is some text";
var pattern = /t/gi;
alert(str.match(pattern).length);

```

Make it shorter.

```
var str = "This is some text";
alert(str.match(/t/gi).length);

```
```
<button onclick="alert(countCharacter('abcdea', 'a'));"> Test! </button>

<script type="text/javascript">
    function countCharacter(sample, characterToFind) {
        var result = 0;

        for(i = 0;i<sample.length;i++) {
            if(sample[i] === characterToFind) {
                result++;
            }
        }

        return result;
    }
</script>

```