Sha256: c416f03aca1d44962b4dd9fc2ed6cdab238acb91f1467a89bbb7121e5614d47b

Contents?: true

Size: 1.75 KB

Versions: 105

Compression:

Stored size: 1.75 KB

Contents

'use strict';

var 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'
};

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

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

function bigPart(number) {
  var factor;
  var result = '';
  for (var 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;
}

function sayDecade(n) {
  for (var decade = 90; decade >= 20; decade -= 10) {
    if (n >= decade) {
      return decades[decade] + '-' + smallNumbers[n - decade];
    }
  }
  throw new Error('Number must be between 10 and 99.');
}

function twoDigit(n) {
  var result;
  if (n < 20) {
    result = smallNumbers[n];
  } else {
    result = sayDecade(n);
  }
  return result;
}

function threeDigit(n) {
  if (n < 100) {
    return twoDigit(n);
  }
  return smallNumbers[Math.floor(n / 100)] + ' hundred ' + twoDigit(n % 100);
}

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

Version data entries

105 entries across 105 versions & 1 rubygems

Version Path
trackler-2.2.1.98 tracks/javascript/exercises/say/example.js
trackler-2.2.1.97 tracks/javascript/exercises/say/example.js
trackler-2.2.1.96 tracks/javascript/exercises/say/example.js
trackler-2.2.1.95 tracks/javascript/exercises/say/example.js
trackler-2.2.1.94 tracks/javascript/exercises/say/example.js
trackler-2.2.1.93 tracks/javascript/exercises/say/example.js
trackler-2.2.1.92 tracks/javascript/exercises/say/example.js
trackler-2.2.1.91 tracks/javascript/exercises/say/example.js
trackler-2.2.1.90 tracks/javascript/exercises/say/example.js
trackler-2.2.1.89 tracks/javascript/exercises/say/example.js
trackler-2.2.1.88 tracks/javascript/exercises/say/example.js
trackler-2.2.1.87 tracks/javascript/exercises/say/example.js
trackler-2.2.1.86 tracks/javascript/exercises/say/example.js
trackler-2.2.1.85 tracks/javascript/exercises/say/example.js
trackler-2.2.1.84 tracks/javascript/exercises/say/example.js
trackler-2.2.1.83 tracks/javascript/exercises/say/example.js
trackler-2.2.1.82 tracks/javascript/exercises/say/example.js
trackler-2.2.1.81 tracks/javascript/exercises/say/example.js
trackler-2.2.1.80 tracks/javascript/exercises/say/example.js
trackler-2.2.1.79 tracks/javascript/exercises/say/example.js