Sha256: 6b328b8f699d104821f1d2c7fe1f63f32fd35226b1908e5f46be53a752f9c9c0

Contents?: true

Size: 938 Bytes

Versions: 165

Compression:

Stored size: 938 Bytes

Contents

const ArgumentError = () => ({
  name: 'argument error',
  message: 'oops',
});

const re = new RegExp(/(plus|minus|divided by|multiplied by)+/g);

class Wordy {

  constructor(question) {
    this.numbers = question.match(/[-]{0,1}\d+/g);
    this.operands = question.match(re);
  }

  answer() {
    if (!this.numbers || !this.operands) {
      throw new ArgumentError();
    }
    let ii = 1,
      jj = 0,
      result = +this.numbers[0];

    while (ii < this.numbers.length + 1) {
      const op = this.operands[jj++],
        b = +this.numbers[ii++] || null;
      switch (op) {
        case 'plus' :
          result += b;
          break;
        case 'minus' :
          result -= b;
          break;
        case 'multiplied by' :
          result *= b;
          break;
        case 'divided by' :
          result /= b;
          break;
      }
    }
    return result;
  }
}

export { Wordy as WordProblem, ArgumentError };

Version data entries

165 entries across 165 versions & 1 rubygems

Version Path
trackler-2.2.1.78 tracks/ecmascript/exercises/wordy/example.js
trackler-2.2.1.77 tracks/ecmascript/exercises/wordy/example.js
trackler-2.2.1.76 tracks/ecmascript/exercises/wordy/example.js
trackler-2.2.1.75 tracks/ecmascript/exercises/wordy/example.js
trackler-2.2.1.74 tracks/ecmascript/exercises/wordy/example.js
trackler-2.2.1.73 tracks/ecmascript/exercises/wordy/example.js
trackler-2.2.1.72 tracks/ecmascript/exercises/wordy/example.js
trackler-2.2.1.71 tracks/ecmascript/exercises/wordy/example.js
trackler-2.2.1.70 tracks/ecmascript/exercises/wordy/example.js
trackler-2.2.1.69 tracks/ecmascript/exercises/wordy/example.js
trackler-2.2.1.68 tracks/ecmascript/exercises/wordy/example.js
trackler-2.2.1.67 tracks/ecmascript/exercises/wordy/example.js
trackler-2.2.1.66 tracks/ecmascript/exercises/wordy/example.js
trackler-2.2.1.65 tracks/ecmascript/exercises/wordy/example.js
trackler-2.2.1.64 tracks/ecmascript/exercises/wordy/example.js
trackler-2.2.1.63 tracks/ecmascript/exercises/wordy/example.js
trackler-2.2.1.62 tracks/ecmascript/exercises/wordy/example.js
trackler-2.2.1.61 tracks/ecmascript/exercises/wordy/example.js
trackler-2.2.1.60 tracks/ecmascript/exercises/wordy/example.js
trackler-2.2.1.59 tracks/ecmascript/exercises/wordy/example.js