Created
May 21, 2012 17:04
-
-
Save kaiquewdev/2763298 to your computer and use it in GitHub Desktop.
Decode letters to phone number.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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