Created
October 2, 2014 15:33
-
-
Save al-the-x/a846da8d289912b07932 to your computer and use it in GitHub Desktop.
Practiced Kata: Check Writing from @TheIronYard--Orlando
This file contains 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
var assert = require('assert'); | |
function test(actual, expected, success){ | |
success = success || 'pass!'; | |
assert.equal(actual, expected); | |
console.log(success); | |
} | |
/** | |
* Check Writing | |
* | |
* Given an Number representing money -- $1234.56 -- convert | |
* that into it's string representation in English words. For | |
* example, 1234.56 is "one thousand, two hundred thirty four | |
* and 56/100s", just like you would see on a check. | |
* | |
* In a lot of ways, this is the inverse of the "String Calculator" | |
* problem, so a lot of what you've learned there will be put into | |
* practice here, but backwards. Hooray! | |
*/ | |
/** | |
* Sample Data: | |
* | |
* $ 1234.56 => "one thousand, two hundred thirty four and 56/100s" | |
* $ 123.45 => "one hundred twenty three and 45/100s" | |
* $ 12.34 => "twleve and 34/100s" | |
* $ 1.23 => "one and 23/100s" | |
* | |
* EXTRA CREDIT! | |
* | |
* $ 12,345,678.90 => | |
* "twelve million, three hundred fourty five thousand, six hundred | |
* seventy eight and 90/100s" | |
* | |
* Make up your own, too. | |
*/ | |
function toEnglish(value){ | |
if ( value === 6 ){ | |
return 'six'; | |
} | |
if ( value === 5 ){ | |
return 'five'; | |
} | |
if ( value === 4 ){ | |
return 'four'; | |
} | |
if ( value === 3 ){ | |
return 'three'; | |
} | |
if ( value === 2 ){ | |
return 'two'; | |
} | |
if ( value === 1 ){ | |
return 'one'; | |
} | |
return 'zero'; | |
} | |
test(true, true, 'true is true!'); | |
var testCases = [ | |
[ 0, 'zero' ], | |
[ 1, 'one' ], | |
[ 2, 'two' ], | |
[ 3, 'three' ], | |
[ 4, 'four' ], | |
[ 5, 'five' ], | |
[ 6, 'six' ], | |
]; | |
var index = 0, testCase; | |
var actual, expected; | |
while ( index < testCases.length ){ | |
testCase = testCases[index]; | |
actual = toEnglish(testCase[0]); | |
expected = testCase[1]; | |
test(actual, expected, | |
testCase[0] + ' >> ' + testCase[1] | |
); | |
index++; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment