Skip to content

Instantly share code, notes, and snippets.

@kaiquewdev
Created May 21, 2012 17:04
Show Gist options
  • Save kaiquewdev/2763298 to your computer and use it in GitHub Desktop.
Save kaiquewdev/2763298 to your computer and use it in GitHub Desktop.
Decode letters to phone number.
#!/usr/bin/env node
var phoneDecode = function ( input ) {
var output = '';
var letters = {
ABC: 2,
DEF: 3,
GHI: 4,
JKL: 5,
MNO: 6,
PQRS: 7,
TUV: 8,
WXYZ: 9,
};
if ( input ) {
input = input.split('');
for ( var i = 0, len = input.length; i < len; i++ ) {
for ( var l in letters ) {
if ( l.indexOf( input[ i ] ) > -1 ) {
input[ i ] = letters[ l ];
}
}
}
output = input.join('');
}
return output;
};
console.assert( phoneDecode( '1-HOME-SWEET-HOME' ) === '1-4663-79338-4663', 'Output one not equal.' )
console.assert( phoneDecode( 'MY-MISERABLE-JOB' ) === '69-647372253-562', 'Output two not equal.' )
var main = (function ( args ) {
if ( args.length > 0 ) {
for ( var index in args ) {
if ( index > 1 ) {
console.log( phoneDecode( args[ index ] ) );
}
}
}
} ( process.argv ));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment