Coding Challenge #40.1: Word Counter in JavaScript




In this Coding Challenge, I build a word counting (concordance) web application in JavaScript. The video demonstrates a use of associate arrays, regular expressions, and other techniques previously covered as part of “Programming from A to Z.”

Next Video: https://youtu.be/fxQ0B6BkfKo

http://shiffman.net/a2z/text-analysis/

Course url: http://shiffman.net/a2z/

Support this channel on Patreon: https://patreon.com/codingtrain

Send me your questions and coding challenges!: https://github.com/CodingTrain/Rainbow-Topics

Contact: https://twitter.com/shiffman

GitHub Repo with all the info for Programming from A to Z: https://github.com/shiffman/A2Z-F16

Links discussed in this video:
ITP from Tisch School of the Arts: https://tisch.nyu.edu/itp

Source Code for the all Video Lessons: https://github.com/CodingTrain/Rainbow-Code

p5.js: https://p5js.org/
Processing: https://processing.org

For More Programming from A to Z videos: https://www.youtube.com/user/shiffman/playlists?shelf_id=11&view=50&sort=dd

For More Coding Challenges: https://www.youtube.com/playlist?list=PLRqwX-V7Uu6ZiZxtDDRCi6uhfTH4FilpH

Help us caption & translate this video!

http://amara.org/v/XJ8r/

Original source


24 responses to “Coding Challenge #40.1: Word Counter in JavaScript”

  1. I have a noob question. On the if statement, how does incrementing an 'object "key" equals the increase in count in "value" ? I understand the declaration of value to 1, if the word (aka counts[word] ) is undefined. But if it exists why does counts[word]++ define the value of the key?

  2. You have a HUGE!!!! problem. What if, by accident, you have a word that is part of the prototype? That should break some stuff. What you can do is to append a NULL-char at the beginning. That is, instead of `counts[key]`, you use `counts['' + key]` everywhere where you have to access `key`.

  3. hello everybody, I got this rather tedious way for excluding some words in the counter:
    for (var j = 0; j < keys.length; j++) {
    var k = keys[j];
    if (k != 'the' &&
    k != 'and' &&
    k != 'of' &&
    k != 'to' &&
    k != 'a' &&
    k != 'in') {
    createDiv(" " + k + " " + counts[k]);
    }
    }
    can we make it with array difference in p5? Some shorter solution?

  4. I've noticed that if you just use if (counts[word]) { ….. It is not working properly , infact I got NAN instead of the numbers. So I think (counts[word] === undefined) is necesssary. Can anyone explain the reason for that?

    Another thing I do not understand completely is how the function compare(a,b){…. works.
    What gets passed in and how the ordering is made is quite obscure to me.

    BTW Great Coding Challenge Dan!

  5. Great video and explanation..! Implemented in Perl while listening.. Here's what I came up with and prints sorted by word alphabetically.

    my %Words;
    map { $Words{$_}++ } split(/W+/, lc $ARGV[0]);
    print "$_ => $Words{$_}" foreach sort keys %Words;

Leave a Reply