Skip to content

Instantly share code, notes, and snippets.

@sirgalleto
Last active January 7, 2017 19:16
Show Gist options
  • Save sirgalleto/806392003f389d628bc3da60eaa73dc3 to your computer and use it in GitHub Desktop.
Save sirgalleto/806392003f389d628bc3da60eaa73dc3 to your computer and use it in GitHub Desktop.
Test
// Write a function that receives an integer and returns its string representation
// 1 -> Uno
// 12 -> Doce
// 17 -> Diecisiete // Diez y siete
// 23 -> Veintitres
// 57 -> Cincuenta y siete
// 158 -> Ciento cincuenta y ocho
// 2347 -> Dos mil trescientos cuarenta y siete
// 0 < n < 10,000 (1-9999)
var names = [
{
'0': 'cero',
'1': 'uno',
'2': 'dos',
'3': 'tres',
'4': 'cuatro',
'5': 'cinco',
'6': 'seis',
'7': 'siete',
'8': 'ocho',
'9': 'nueve',
},
{
'1': 'diez',
'2': 'veinte',
'3': 'treinta',
'4': 'cuarenta',
'5': 'cincuenta',
'6': 'sesenta',
'7': 'setenta',
'8': 'ochenta',
'9': 'noventa',
'conector': ' y'
},
{
'1': 'cien',
'9': 'novecientos',
'default': 'cientos'
},
{
'1': 'mil',
'default': 'mil'
},
{
'default': 'mil'
}
]
var exceptions = {
'10': 'diez',
'11': 'once',
'12': 'doce',
'13': 'trece',
'14': 'catorce',
'15': 'quice',
'16': 'dieciseis',
'17': 'diecisiete',
'18': 'dieciocho',
'19': 'diecinueve',
'21': 'veintiuno',
'22': 'veintidos',
'23': 'veintitres',
'24': 'veinticuatro',
'25': 'veinticinco',
'26': 'veintiseis',
'27': 'veinticiete',
'28': 'veintiocho',
'29': 'veintinueve'
//.. 2929
}
// 1347
function integerToStringRepresentation(number) {
// Asing
number = String(number);
pre = number[0]; //1
post = number.substring(1, number.length); //"347"
postLength = post.length; //3
//Calc
var compose = '';
if(exceptions[number]) {
compose += exceptions[number];
return compose;
}
else if(names[postLength][pre]) {
compose += names[postLength][pre];
}
else {
compose += names[0][pre] + names[postLength]['default'];
}
// Validate & Compose || response
if(postLength === 0 || Number(post) === 0) {
return compose;
}
else {
if(names[postLength]['conector']){
compose += names[postLength]['conector'];
}
return compose + ' ' + integerToStringRepresentation(Number(post));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment