From 138834821b8cb7f6ab3b195aacf580cc79117ad3 Mon Sep 17 00:00:00 2001 From: sezalcruz Date: Sat, 23 Jan 2016 22:37:01 -0400 Subject: [PATCH] Implemented solucion to problem --- .gitignore | 2 + int_to_words.js | 132 ++++++++++++++++++++++++++++++++++++++ package.json | 14 +++++ test/test.js | 164 ++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 312 insertions(+) create mode 100644 .gitignore create mode 100644 int_to_words.js create mode 100644 package.json create mode 100644 test/test.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..03e05e4 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.DS_Store +/node_modules diff --git a/int_to_words.js b/int_to_words.js new file mode 100644 index 0000000..d2b66d8 --- /dev/null +++ b/int_to_words.js @@ -0,0 +1,132 @@ +var ones = { + 0 : '', + 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 ' + }, + + tens = { + 2 : 'twenty ', + 3 : 'thirty ', + 4 : 'fourty ', + 5 : 'fifty ', + 6 : 'sixty ', + 7 : 'seventy ', + 8 : 'eighty ', + 9 : 'ninety ' + }, + + scale = { + 0 : '', + 1 : 'thousand ', + 2 : 'million ' + } + +exports.int_to_words = function(params){ + + if(params === 0){ + return 'zero'; + } + + var numberPosition = 0, magnitude = 1, periods = 0, words = ''; + + var numbers = splitNumbers(params).reverse(); //Give me an array of the numbers, reversed order. + + var chunks = splitChunks(numbers); + + while(chunks.length > 0){ + + var temp = solve(chunks.pop()); + + if(temp !== ''){ + + temp += scale[periods]; + } + + words = temp + words; + + periods++; + + } + + return words.trim(); +} + + +function solve(chunk){ + + var result = ''; + + if(chunk.length === 3){ + + for (var i = 0; i < chunk.length; i++) { + + if(chunk[i] > 0 && i === 0){ + result = result + ones[chunk[i]] + 'hundred '; + } else if(chunk[i] === 1 && i === 1){ + result = result + ones[(chunk[i] * 10) + chunk[i + 1]]; + break; + } else if(chunk[i] > 1 && i === 1){ + result = result + tens[chunk[i]]; + } else if(chunk[i] > 0 && i === 2){ + result = result + ones[chunk[i]]; + } else { + continue; + } + }; + + } else if(chunk.length === 2){ + + if(chunk[0] === 1){ + + result = ones[(chunk[0] * 10) + chunk[1]]; + + } else if(chunk[0] > 1){ + + result = tens[chunk[0]] + ones[chunk[1]]; + + } + + } else { + result = ones[chunk[0]]; + } + + return result; +} + +function splitChunks(numbers){ + var final = []; + + while(numbers.length > 0){ + final.push(numbers.splice(0, 3).reverse()); + } + + return final.reverse(); + +} + +function splitNumbers(number){ + + var numberArray = number.toString(10).split("").map(Number); + + return numberArray; +} + + + diff --git a/package.json b/package.json new file mode 100644 index 0000000..1e14db8 --- /dev/null +++ b/package.json @@ -0,0 +1,14 @@ +{ + "name": "int-to-words", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "test": "mocha" + }, + "author": "", + "license": "ISC", + "dependencies": { + "mocha": "^2.3.4" + } +} diff --git a/test/test.js b/test/test.js new file mode 100644 index 0000000..4e7c084 --- /dev/null +++ b/test/test.js @@ -0,0 +1,164 @@ +var assert = require('assert'), + int_to_words = require('../int_to_words').int_to_words; +describe('Testing int_to_words converter', function(){ + + describe('0 - 999', function(){ + + it('should return zero', function(done){ + + assert.equal('zero', int_to_words(0)); + done(); + }); + + it('should return one', function(done){ + + assert.equal('one', int_to_words(1)); + done(); + }); + + it('should return nine', function(done){ + + assert.equal('nine', int_to_words(9)); + done(); + }); + + it('should return ten', function(done){ + + assert.equal('ten', int_to_words(10)); + done(); + }); + + it('should return fifteen', function(done){ + + assert.equal('fifteen', int_to_words(15)); + done(); + }); + + it('should return fifty', function(done){ + + assert.equal('fifty', int_to_words(50)); + done(); + }); + + it('should return ninety nine', function(done){ + + assert.equal('ninety nine', int_to_words(99)); + done(); + }); + + it('should return one hundred', function(done){ + + assert.equal('one hundred', int_to_words(100)); + done(); + }); + + it('should return one hundred one', function(done){ + + assert.equal('one hundred one', int_to_words(101)); + done(); + }); + + it('should return one hundred fifty', function(done){ + + assert.equal('one hundred fifty', int_to_words(150)); + done(); + }); + + it('should return two hundred', function(done){ + + assert.equal('two hundred', int_to_words(200)); + done(); + }); + + it('should return two hundred eleven', function(done){ + + assert.equal('two hundred eleven', int_to_words(211)); + done(); + }); + + it('should return five hundred fifty one', function(done){ + + assert.equal('five hundred fifty one', int_to_words(551)); + done(); + }); + + it('should return six hundred nineteen', function(done){ + + assert.equal('six hundred nineteen', int_to_words(619)); + done(); + }); + + + it('should return nine hundred seventy five', function(done){ + + assert.equal('nine hundred seventy five', int_to_words(975)); + done(); + }); + + it('should return nine hundred ninety nine', function(done){ + + assert.equal('nine hundred ninety nine', int_to_words(999)); + done(); + }); + }); + + describe('cheking the periods', function(){ + + it('should return one thousand', function(done){ + + assert.equal('one thousand', int_to_words(1000)); + done(); + }); + + it('should return thirteen thousand', function(done){ + + assert.equal('thirteen thousand', int_to_words(13000)); + done(); + }); + + it('should return thirteen thousand one', function(done){ + + assert.equal('thirteen thousand one', int_to_words(13001)); + done(); + }); + + it('should return one million', function(done){ + + assert.equal('one million', int_to_words(1000000)); + done(); + }); + + it('should return nine hundred ninety nine million nine hundred ninety nine thousand nine hundred ninety nine', function(done){ + + assert.equal('nine hundred ninety nine million nine hundred ninety nine thousand nine hundred ninety nine', int_to_words(999999999)); + done(); + }); + }); + + describe('Provided Examples', function(){ + + it('should return four thousand five hundred twenty seven', function(done){ + + assert.equal('four thousand five hundred twenty seven', int_to_words(4527)); + done(); + }); + + it('should return two hundred thousand seventeen', function(done){ + + assert.equal('two hundred thousand seventeen', int_to_words(200017)); + done(); + }); + + it('should return seven hundred eighty four', function(done){ + + assert.equal('seven hundred eighty four', int_to_words(784)); + done(); + }); + + it('should return one million two hundred fifteen', function(done){ + + assert.equal('one million two hundred fifteen', int_to_words(1000215)); + done(); + }); + }); +}); \ No newline at end of file