Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.DS_Store
/node_modules
132 changes: 132 additions & 0 deletions int_to_words.js
Original file line number Diff line number Diff line change
@@ -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;
}



14 changes: 14 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
164 changes: 164 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -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();
});
});
});