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.119 tracks/javascript/exercises/wordy/example.js
trackler-2.2.1.118 tracks/javascript/exercises/wordy/example.js
trackler-2.2.1.117 tracks/javascript/exercises/wordy/example.js
trackler-2.2.1.116 tracks/javascript/exercises/wordy/example.js
trackler-2.2.1.115 tracks/javascript/exercises/wordy/example.js
trackler-2.2.1.114 tracks/javascript/exercises/wordy/example.js
trackler-2.2.1.113 tracks/javascript/exercises/wordy/example.js
trackler-2.2.1.111 tracks/javascript/exercises/wordy/example.js
trackler-2.2.1.110 tracks/javascript/exercises/wordy/example.js
trackler-2.2.1.109 tracks/javascript/exercises/wordy/example.js
trackler-2.2.1.108 tracks/javascript/exercises/wordy/example.js
trackler-2.2.1.107 tracks/javascript/exercises/wordy/example.js
trackler-2.2.1.106 tracks/javascript/exercises/wordy/example.js
trackler-2.2.1.105 tracks/javascript/exercises/wordy/example.js
trackler-2.2.1.104 tracks/javascript/exercises/wordy/example.js
trackler-2.2.1.103 tracks/javascript/exercises/wordy/example.js
trackler-2.2.1.102 tracks/javascript/exercises/wordy/example.js
trackler-2.2.1.101 tracks/javascript/exercises/wordy/example.js
trackler-2.2.1.100 tracks/javascript/exercises/wordy/example.js
trackler-2.2.1.99 tracks/javascript/exercises/wordy/example.js