Sha256: de27c29a4a4c1042999fcdefe99e1c31c1675147fb27a6d5b6076c7dae31cbd8

Contents?: true

Size: 1.08 KB

Versions: 20

Compression:

Stored size: 1.08 KB

Contents

class ArgumentError extends Error {
  constructor() {
    super();
    this.name = 'argument error';
    this.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;
  }
}

String.prototype.combine = function(x){
  return x;
}

let f = new String('abc');
let g = f.combine('xyz');


export { Wordy as WordProblem, ArgumentError };

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/ecmascript/exercises/wordy/example.js
trackler-2.2.1.179 tracks/ecmascript/exercises/wordy/example.js
trackler-2.2.1.178 tracks/ecmascript/exercises/wordy/example.js
trackler-2.2.1.177 tracks/ecmascript/exercises/wordy/example.js
trackler-2.2.1.176 tracks/ecmascript/exercises/wordy/example.js
trackler-2.2.1.175 tracks/ecmascript/exercises/wordy/example.js
trackler-2.2.1.174 tracks/ecmascript/exercises/wordy/example.js
trackler-2.2.1.173 tracks/ecmascript/exercises/wordy/example.js
trackler-2.2.1.172 tracks/ecmascript/exercises/wordy/example.js
trackler-2.2.1.171 tracks/ecmascript/exercises/wordy/example.js
trackler-2.2.1.170 tracks/ecmascript/exercises/wordy/example.js
trackler-2.2.1.169 tracks/ecmascript/exercises/wordy/example.js
trackler-2.2.1.167 tracks/ecmascript/exercises/wordy/example.js
trackler-2.2.1.166 tracks/ecmascript/exercises/wordy/example.js
trackler-2.2.1.165 tracks/ecmascript/exercises/wordy/example.js
trackler-2.2.1.164 tracks/ecmascript/exercises/wordy/example.js
trackler-2.2.1.163 tracks/ecmascript/exercises/wordy/example.js
trackler-2.2.1.162 tracks/ecmascript/exercises/wordy/example.js
trackler-2.2.1.161 tracks/ecmascript/exercises/wordy/example.js
trackler-2.2.1.160 tracks/ecmascript/exercises/wordy/example.js