DEV Community

loading...

Discussion on: Daily Challenge #248 - Chinese Numerals

Collapse
savagepixie profile image
SavagePixie • Edited

I didn't quite get what the deal was with the zero, and since there were no examples to show how it worked, I decided to do Japanese numerals, which are basically the same but without the zero. Here's my JavaScript implementation. It could probably be a lot simpler, but oh well.

const numerals = {
    0: '0',
    1: '',
    2: '',
    3: '',
    4: '',
    5: '',
    6: '',
    7: '',
    8: '',
    9: '',
}

const units = {
    0: '',
    1: '',
    2: '',
    3: '',
    4: '',
}

const toJapaneseNumeral = n => n
    .toString()
    .split('')
    .reverse()
    .map((x, i) => numerals[x] + units[i])
    .reverse()
    .join('')
    .replace('一十', '')
    .replace(/0.?/g, '')
Collapse
citizen428 profile image
Michael Kohl

I didn't quite get what the deal was with the zero

Wikipedia has a decent explanation: "Interior zeroes before the unit position (as in 1002) must be spelt explicitly. The reason for this is that trailing zeroes (as in 1200) are often omitted as shorthand, so ambiguity occurs. One zero is sufficient to resolve the ambiguity. Where the zero is before a digit other than the units digit, the explicit zero is not ambiguous and is therefore optional, but preferred. "

There are a few examples too, but they use traditional instead of simplified characters.