Sha256: eb92f8bafc08dc1e603c56abed0e81923c44a77413a0b118096e7b7e4dd00072

Contents?: true

Size: 1.64 KB

Versions: 211

Compression:

Stored size: 1.64 KB

Contents

const smallNumbers = {
  0:  'zero',
  1:  'one',
  2:  'two',
  3:  'three',
  4:  'four',
  5:  'five',
  6:  'six',
  7:  'seven',
  8:  'eight',
  9:  'nine',
  10: 'ten',
  11: 'eleven',
  12: 'twelve',
  13: 'thirteen',
  14: 'fourteen',
  15: 'fifteen',
  16: 'sixteen',
  17: 'seventeen',
  18: 'eighteen',
  19: 'nineteen'
};

const decades = {
  20: 'twenty',
  30: 'thirty',
  40: 'forty',
  50: 'fifty',
  60: 'sixty',
  70: 'seventy',
  80: 'eighty',
  90: 'ninety'
};

const bigNumbers = {
  1000:       'thousand',
  1000000:    'million',
  1000000000: 'billion'
};

function bigPart(number) {
  let factor, result = '';
  for (let bigNumber = 1000000000; bigNumber >= 1000; bigNumber /= 1000) {
    if (number.current >= bigNumber) {
      factor = Math.floor(number.current / bigNumber);
      result += threeDigit(factor) + ' ' + bigNumbers[bigNumber] + ' ';
      number.current = number.current - factor * bigNumber;
    }
  }
  return result;
}

const sayDecade = n => {
  for (let decade = 90; decade >= 20; decade -= 10) {
    if (n >= decade) {
      return decades[decade] + '-' + smallNumbers[n - decade];
    }
  }
};

const twoDigit = n => n < 20 ? smallNumbers[n] : sayDecade(n);

const threeDigit = n => n < 100 ? twoDigit(n)
  : smallNumbers[Math.floor(n / 100)] + ' hundred ' + twoDigit(n % 100);

export default class Say {
  inEnglish(n) {
    let result;
    const number = {current: n};

    if (0 <= n && n < 1000000000000) {
      result = bigPart(number);
      result += threeDigit(number.current);
      return result.replace(/.zero/, '');
    } else {
      throw new Error('Number must be between 0 and 999,999,999,999.');
    }
  }
}

Version data entries

211 entries across 211 versions & 1 rubygems

Version Path
trackler-2.2.0.0 tracks/ecmascript/exercises/say/example.js
trackler-2.1.0.55 tracks/ecmascript/exercises/say/example.js
trackler-2.1.0.54 tracks/ecmascript/exercises/say/example.js
trackler-2.1.0.53 tracks/ecmascript/exercises/say/example.js
trackler-2.1.0.52 tracks/ecmascript/exercises/say/example.js
trackler-2.1.0.51 tracks/ecmascript/exercises/say/example.js
trackler-2.1.0.50 tracks/ecmascript/exercises/say/example.js
trackler-2.1.0.49 tracks/ecmascript/exercises/say/example.js
trackler-2.1.0.48 tracks/ecmascript/exercises/say/example.js
trackler-2.1.0.47 tracks/ecmascript/exercises/say/example.js
trackler-2.1.0.46 tracks/ecmascript/exercises/say/example.js
trackler-2.1.0.45 tracks/ecmascript/exercises/say/example.js
trackler-2.1.0.44 tracks/ecmascript/exercises/say/example.js
trackler-2.1.0.43 tracks/ecmascript/exercises/say/example.js
trackler-2.1.0.42 tracks/ecmascript/exercises/say/example.js
trackler-2.1.0.41 tracks/ecmascript/exercises/say/example.js
trackler-2.1.0.40 tracks/ecmascript/exercises/say/example.js
trackler-2.1.0.39 tracks/ecmascript/exercises/say/example.js
trackler-2.1.0.38 tracks/ecmascript/exercises/say/example.js
trackler-2.1.0.37 tracks/ecmascript/exercises/say/example.js