DEV Community

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, '')