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.139 tracks/javascript/exercises/say/example.js
trackler-2.2.1.138 tracks/javascript/exercises/say/example.js
trackler-2.2.1.137 tracks/javascript/exercises/say/example.js
trackler-2.2.1.136 tracks/javascript/exercises/say/example.js
trackler-2.2.1.135 tracks/javascript/exercises/say/example.js
trackler-2.2.1.134 tracks/javascript/exercises/say/example.js
trackler-2.2.1.133 tracks/javascript/exercises/say/example.js
trackler-2.2.1.132 tracks/javascript/exercises/say/example.js
trackler-2.2.1.131 tracks/javascript/exercises/say/example.js
trackler-2.2.1.130 tracks/javascript/exercises/say/example.js
trackler-2.2.1.129 tracks/javascript/exercises/say/example.js
trackler-2.2.1.128 tracks/javascript/exercises/say/example.js
trackler-2.2.1.127 tracks/javascript/exercises/say/example.js
trackler-2.2.1.126 tracks/javascript/exercises/say/example.js
trackler-2.2.1.125 tracks/javascript/exercises/say/example.js
trackler-2.2.1.124 tracks/javascript/exercises/say/example.js
trackler-2.2.1.123 tracks/javascript/exercises/say/example.js
trackler-2.2.1.122 tracks/javascript/exercises/say/example.js
trackler-2.2.1.121 tracks/javascript/exercises/say/example.js
trackler-2.2.1.120 tracks/javascript/exercises/say/example.js