Sha256: 48f4f2e36f12f96c9689ee4c07ccbdce081322533b9345238af82407a54c59ff

Contents?: true

Size: 1.64 KB

Versions: 110

Compression:

Stored size: 1.64 KB

Contents

'use strict';

exports.WordProblem   = WordProblem;
exports.ArgumentError = ArgumentError;

var BINARY_OPERATORS = {
  'plus': function (l, r) { return l + r; },
  'minus': function (l, r) { return l - r; },
  'multiplied by': function (l, r) { return l * r; },
  'divided by': function (l, r) { return l / r; }
};

function operators() {
  return Object.keys(BINARY_OPERATORS);
}

function pattern() {
  var expression = '';
  var operations = ' (' + operators().join('|') + ') ';

  expression += '(?:what is ([-+]?[\\d]+)';
  expression += operations;
  expression += '([-+]?[\\d]+)(?:';
  expression += operations;
  expression += '([-+]?[\\d]+))?)';

  return new RegExp(expression, 'i');
}

function WordProblem(question) {
  this.question = question || '';
  this.matches  = this.question.match(pattern());
}

WordProblem.prototype.tooComplicated = function () {
  return this.matches === null;
};

WordProblem.prototype.answer = function () {
  if (this.tooComplicated()) {
    throw new ArgumentError('I don\'t understand the question');
  }
  return this.evaluate();
};

WordProblem.prototype.evaluate = function () {
  var out = 0;
  var m   = this.matches;

  if ( (typeof m[1]) === 'string'  && (typeof m[2]) === 'string'  && (typeof m[3]) === 'string') {
    out = this.operate(m[2], m[1], m[3]);
  }

  if ( (typeof m[4]) === 'string' && (typeof m[5]) === 'string') {
    out = this.operate(m[4], out, m[5]);
  }

  return out;
};

WordProblem.prototype.operate = function (operation, l, r) {
  var fn = BINARY_OPERATORS[operation] || function () { return 0; };
  return fn(Number(l), Number(r));
};

function ArgumentError() {}
ArgumentError.prototype = new Error();

Version data entries

110 entries across 110 versions & 1 rubygems

Version Path
trackler-2.2.1.159 tracks/javascript/exercises/wordy/example.js
trackler-2.2.1.158 tracks/javascript/exercises/wordy/example.js
trackler-2.2.1.157 tracks/javascript/exercises/wordy/example.js
trackler-2.2.1.156 tracks/javascript/exercises/wordy/example.js
trackler-2.2.1.155 tracks/javascript/exercises/wordy/example.js
trackler-2.2.1.154 tracks/javascript/exercises/wordy/example.js
trackler-2.2.1.153 tracks/javascript/exercises/wordy/example.js
trackler-2.2.1.152 tracks/javascript/exercises/wordy/example.js
trackler-2.2.1.151 tracks/javascript/exercises/wordy/example.js
trackler-2.2.1.150 tracks/javascript/exercises/wordy/example.js
trackler-2.2.1.149 tracks/javascript/exercises/wordy/example.js
trackler-2.2.1.148 tracks/javascript/exercises/wordy/example.js
trackler-2.2.1.147 tracks/javascript/exercises/wordy/example.js
trackler-2.2.1.146 tracks/javascript/exercises/wordy/example.js
trackler-2.2.1.145 tracks/javascript/exercises/wordy/example.js
trackler-2.2.1.144 tracks/javascript/exercises/wordy/example.js
trackler-2.2.1.143 tracks/javascript/exercises/wordy/example.js
trackler-2.2.1.142 tracks/javascript/exercises/wordy/example.js
trackler-2.2.1.141 tracks/javascript/exercises/wordy/example.js
trackler-2.2.1.140 tracks/javascript/exercises/wordy/example.js