Created
October 7, 2014 14:34
-
-
Save al-the-x/507e3eb14a117ded1c53 to your computer and use it in GitHub Desktop.
Check Writing + forEach re @TheIronYard--Orlando Coding Dojos
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
| var assert = require('assert'); | |
| function test(actual, expected, success){ | |
| success = success || 'pass!'; | |
| assert(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){ | |
| var ones = [ 'zero', 'one', 'two', 'three', /* . . . */ ]; | |
| var tens = [ 'ten', 'twenty', 'thirty', 'forty', /* . . . */ ]; | |
| // 123.45 >> 'one hundred twenty three and 45/100s' | |
| // 123.45 >> ensure 2 decimal places AND convert to String >> 123.45 | |
| // 123 >> ensure 2 decimal places AND convert to String >> 123.00 | |
| value = Number(value).toFixed(2); | |
| // '123.45' | |
| // '123.00' | |
| // '1234.56' | |
| // '12345.67' | |
| // '1.00' | |
| // '42.00' | |
| // '7247.86' | |
| // Using String.slice()... | |
| var cents = value.slice(-2); | |
| var dollars = value.slice(0, -3); | |
| var english = cents + '/100s dollars'; | |
| /* Then a miracle happens */ | |
| { /* . . . */ } | |
| return english; | |
| } | |
| var testCases = [ | |
| [0, 'zero' ], | |
| [1, 'one' ], | |
| [2, 'two' ], | |
| [3, 'three' ], | |
| [123, 'one hundred twenty three' ] | |
| ]; | |
| { // BLOCK OF CONVENIENCE | |
| var actual, expected; | |
| for ( var index = 0; index < testCases.length; index++ ){ | |
| actual = toEnglish(testCases[index][0]); | |
| expected = testCases[index][1]; | |
| assert.strictEqual(actual, expected); | |
| } | |
| actual; // {toEnglish(123)} | |
| expected; // {'one hundred twenty three'} | |
| } // END BLOCK OF CONVENIENCE | |
| testCases.forEach(function(testCase){ | |
| var actual = toEnglish(testCase[0]), | |
| expected = testCase[1]; | |
| assert.strictEqual(toEnglish(testCase[0]), testCase[1], | |
| testCase[0] + ' >> ' + testCase[1]); | |
| }); | |
| // Assuming the `for` loop above did not exist... | |
| actual; // {undefined} | |
| expected; // {undefined} | |
| test(1, 'one and 00/100s dollars'); | |
| test(2, 'two and 00/100s dollars'); | |
| test(3, 'three and 00/100s dollars'); | |
| test(4, 'four and 00/100s dollars'); | |
| test(5, 'five and 00/100s dollars'); | |
| test(1.23, 'one and 23/100s dollars'); | |
| test(12.34, 'twelve and 34/100s dollars'); | |
| test(123.45, 'one hundred twenty three and 45/100s dollars'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment