<commit_before>---
layout: post
title: Exercism.io - Word Count
---

I did the JavaScript Word Count exercise on [Exercism.io](http://exercism.io) tonight.  It was a pretty interesting
little exercise.  Basically, you accept a string and count the number of times each word appears.

So calling it like this:
```JavaScript
words("test another test string");
```

Would result in the following return:
```JavaScript
{ "test": 2, "another": 1, "string": 1 }
```

I ended up solving it this way:
```JavaScript
var words = function(input) {
    var subStrings = input.replace(/\s+/, ' ').split(' ');
    var total = Object.create(null);

    subStrings.forEach(function(string) {
        if (typeof total[string] === 'undefined') {
            total[string] = 0;
        }

        total[string]++;
    });

    return total;
};

module.exports = words;
```

Pretty straightforward, I thought.

Other folks on the site had some good feedback.  Initially, I was using `var total = {}` which led to some additional
complexity in the forEach loop to test invalid types.  Based on some comments, I switched to `Object.create(null)` which
allows you to create an object with no prototype.

Other people on the site used `Array.reduce()` to solve the problem.  That was interesting to see.  I haven't really used
`Array.reduce()` before; I'll have to look into it more.

One person commented on the use of `Array.forEach()` citing issues with legibility and backwards compatibility.  I feel
that it's pretty easy to read, and easier in some ways than a normal for loop.  And it's supported by IE9 and up, which
is plenty of backwards compatibility for a demo exercise.  I think we need to push ahead with newer capibilities of JavaScript
and not let support for old browsers hamper the evolution of the language, unless backwards compatibility really is a 
deal breaker.
<commit_msg>Change highlighter syntax<commit_after>---
layout: post
title: Exercism.io - Word Count
---

I did the JavaScript Word Count exercise on [Exercism.io](http://exercism.io) tonight.  It was a pretty interesting
little exercise.  Basically, you accept a string and count the number of times each word appears.

So calling it like this:
{% highlight javascript %}
words("test another test string");
{% endhighlight %}

Would result in the following return:
{% highlight javascript %}
{ "test": 2, "another": 1, "string": 1 }
{% endhighlight %}

I ended up solving it this way:
{% highlight javascript %}
var words = function(input) {
    var subStrings = input.replace(/\s+/, ' ').split(' ');
    var total = Object.create(null);

    subStrings.forEach(function(string) {
        if (typeof total[string] === 'undefined') {
            total[string] = 0;
        }

        total[string]++;
    });

    return total;
};

module.exports = words;
{% endhighlight %}

Pretty straightforward, I thought.

Other folks on the site had some good feedback.  Initially, I was using `var total = {}` which led to some additional
complexity in the forEach loop to test invalid types.  Based on some comments, I switched to `Object.create(null)` which
allows you to create an object with no prototype.

Other people on the site used `Array.reduce()` to solve the problem.  That was interesting to see.  I haven't really used
`Array.reduce()` before; I'll have to look into it more.

One person commented on the use of `Array.forEach()` citing issues with legibility and backwards compatibility.  I feel
that it's pretty easy to read, and easier in some ways than a normal for loop.  And it's supported by IE9 and up, which
is plenty of backwards compatibility for a demo exercise.  I think we need to push ahead with newer capibilities of JavaScript
and not let support for old browsers hamper the evolution of the language, unless backwards compatibility really is a 
deal breaker.

{'fraction_non_alphanumeric': 0.09109838625715773, 'fraction_numerical': 0.0026028110359187923, 'mean_word_length': 3.779850746268657, 'pattern_counts': {'":': 6, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2352544', 'n_tokens_mistral': 1076, 'n_tokens_neox': 1001, 'n_words': 516}