Answer

Answer

Senior JavaScript Developer: t.me/js_test

Ответ:

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()
{
  this.reset();
  var prevChar = ' ';
  var chunk = this.reader.getChunk();

  while (chunk !== '') {

    for (var i = 0; i < chunk.length; i++) {
      if (chunk[i] === '\n') {
        this.lineCount++;
        prevChar = ' ';
      } else {
        this.charCount++;
        if (this.lineCount === 0) this.lineCount = 1;
        if (chunk[i] !== ' ' && prevChar === ' ') {
          this.wordCount++;
        }
        prevChar = chunk[i];
      }
    }
    chunk = this.reader.getChunk();
  }
};

Report Page