Skip to content

Instantly share code, notes, and snippets.

@railsstudent
Created January 7, 2017 16:57
Show Gist options
  • Save railsstudent/05097ddcbd50fb97d111706674c68cb6 to your computer and use it in GitHub Desktop.
Save railsstudent/05097ddcbd50fb97d111706674c68cb6 to your computer and use it in GitHub Desktop.
function DocumentParser(reader)
{
this.reader = reader;
this.reset();
}
DocumentParser.prototype.reset = function()
{
this.wordCount = 0;
this.charCount = 0;
this.lineCount = 0;
};
DocumentParser.prototype.parse = function()
{
// TODO: Code Here!
var chunk = this.reader.getChunk();
var ch = null;
var prevCh = null;
while (chunk !== "") {
for (var i = 0; i < chunk.length; i++) {
var prevCh = ch;
ch = chunk[i];
if (ch !== '\n') {
this.charCount += 1;
}
if ( (ch === ' ' || ch === '\n') && prevCh !== ' ' && prevCh !== '\n' && prevCh != null) { // reach the boundary of word
this.wordCount += 1;
}
if (ch === '\n') {
this.lineCount += 1;
}
};
chunk = this.reader.getChunk();
}
if (ch != null && ch !== '' && ch !== '\n' && ch !== ' ') {
this.wordCount += 1;
}
if ((ch != null && ch !== '') || ch === '\n') {
this.lineCount += 1;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment