Last active
December 21, 2015 16:28
-
-
Save noniq/6333541 to your computer and use it in GitHub Desktop.
Convert numbers from 1 to 999999 into german strings. Script should prompt for a number and show result using an JavaScript-alert. Must run in Chrome out of the box, just by loading the HTML file.
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
<pre> | |
<script> | |
function human_nr(n){ | |
// Make `prompt` and `alert` no-ops when run in test mode | |
prompt = function() { return n }; | |
alert = function() {}; | |
// To extract the bare, minimized, executable script: | |
// * Copy the lines between the markers into a new file | |
// * Surround with script tag. | |
// | |
// To determine the size of the extracted script: | |
// * Count the number of characters between the markers (including the | |
// final newline). Make sure that UTF-8 multibyte characters are counted | |
// as one character, not as several bytes. TextMate’s Ctrl-n will count | |
// bytes – use eg. `wc -m` instead. | |
// * Add 16 to account for script tags and removal of final newline. | |
// | |
// Currently, there are 358 characters between the markers, so the extracted | |
// script will be 374 characters long. | |
// | |
// See also: | |
// * https://github.com/catearcher/HumanNumbers | |
// * https://gist.github.com/neuling/6334065 | |
// | |
// ---8<---8<---8<---8<---8<---8<---8<--- | |
n=prompt(T="0Xn0zwX0drX0vier0fünf0sechs0sieben0acht0neun0z0(en|s)z0zehn0Y0ei0X0elf0XnY0zwölf0zwXY0wanz0wXß0Xßi0Xzi0Xns0Xn$0Y0Xnzig00^t.+?d".split(0)) | |
function d(n){r=T[n/100%10^0] | |
r+=(r?"hundert":"")+T[n%10] | |
n%=100 | |
return r+(r&&n>19?"und":"")+(n>9?T[n/10^0]+"zig":"")}r=d(n/1e3^0)+"tausend"+d(n) | |
for(i=29;i>9;)r=r.replace(RegExp(T[i--],"g"),T[i--]) | |
alert(r) | |
// ---8<---8<---8<---8<---8<---8<---8<--- | |
return r | |
}; | |
var good = 0, bad = 0; | |
assert(1, "eins"); | |
assert(2, "zwei"); | |
assert(3, "drei"); | |
assert(4, "vier"); | |
assert(5, "fünf"); | |
assert(6, "sechs"); | |
assert(7, "sieben"); | |
assert(8, "acht"); | |
assert(9, "neun"); | |
assert(10, "zehn"); | |
assert(11, "elf"); | |
assert(12, "zwölf"); | |
assert(13, "dreizehn"); | |
assert(14, "vierzehn"); | |
assert(16, "sechzehn"); | |
assert(17, "siebzehn"); | |
assert(18, "achtzehn"); | |
assert(19, "neunzehn"); | |
assert(20, "zwanzig"); | |
assert(21, "einundzwanzig"); | |
assert(22, "zweiundzwanzig"); | |
assert(27, "siebenundzwanzig"); | |
assert(30, "dreißig"); | |
assert(31, "einunddreißig"); | |
assert(40, "vierzig"); | |
assert(42, "zweiundvierzig"); | |
assert(50, "fünfzig"); | |
assert(53, "dreiundfünfzig"); | |
assert(60, "sechzig"); | |
assert(64, "vierundsechzig"); | |
assert(70, "siebzig"); | |
assert(77, "siebenundsiebzig"); | |
assert(80, "achtzig"); | |
assert(88, "achtundachtzig"); | |
assert(90, "neunzig"); | |
assert(99, "neunundneunzig"); | |
assert(100, "einhundert"); | |
assert(101, "einhunderteins"); | |
assert(107, "einhundertsieben"); | |
assert(112, "einhundertzwölf"); | |
assert(117, "einhundertsiebzehn"); | |
assert(181, "einhunderteinundachtzig"); | |
assert(200, "zweihundert"); | |
assert(267, "zweihundertsiebenundsechzig"); | |
assert(700, "siebenhundert"); | |
assert(766, "siebenhundertsechsundsechzig"); | |
assert(999, "neunhundertneunundneunzig"); | |
assert(1000, "eintausend"); | |
assert(1001, "eintausendeins"); | |
assert(1011, "eintausendelf"); | |
assert(1555, "eintausendfünfhundertfünfundfünfzig"); | |
assert(1990, "eintausendneunhundertundneunzig"); | |
assert(2500, "zweitausendfünfhundert"); | |
assert(10000, "zehntausend"); | |
assert(11111, "elftausendeinhundertelf"); | |
assert(69666, "neunundsechzigtausendsechshundertsechsundsechzig"); | |
assert(100000, "einhunderttausend"); | |
assert(100001, "einhunderttausendeins"); | |
assert(101001, "einhunderteintausendeins"); | |
assert(111111, "einhundertelftausendeinhundertelf"); | |
assert(218519, "zweihundertachtzehntausendfünfhundertneunzehn"); | |
assert(878777, "achthundertachtundsiebzigtausendsiebenhundertsiebenundsiebzig"); | |
assert(999999, "neunhundertneunundneunzigtausendneunhundertneunundneunzig"); | |
function assert(nr, expected) { | |
var result = human_nr(nr); | |
if (result == expected) { | |
good++; | |
return; | |
} | |
bad++; | |
document.write("FAILED: Expected c(<b>" + nr + "</b>) to return '<b>" + expected + "</b>', got '<b>" + result + "</b>'.\n"); | |
} | |
document.write("\n\n<b style='color: #090'>" + good + " good</b>, <b style='color: #900'>" + bad + " bad.</b>"); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment