## DEV Community is a community of 616,766 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

# Discussion on: Daily Challenge #248 - Chinese Numerals

Peter Lau

``````// 28901 二万八千九百零一
// 28911 二万八千九百一十一
// 20911 二万零九百一十一
// 20901 二万零九百零一
// 20001 二万零一
// 29000 二万九

let numberArray = [
'零',
'一',
'二',
'三',
'四',
'五',
'六',
'七',
'八',
'九',
]

let unitArray = ['', '十', '百', '千', '万']

const toChineseNumeral = input => {
return input.toString().split('').map(
(number, idx) => {
return numberArray[parseInt(number)]
}
).reduce(
(acc, current) => {
let last = acc[acc.length - 1]
if (last == '零' && current == '零')
acc[acc.length - 1] = ' '
if (acc.length == (input.length - 1) && current == '零')
current = ''
acc.push(current)
return acc
},
[]
).map(
(elem, idx) => {
if (elem == ' ' || elem == '零')
return elem.trim()
return elem + unitArray[input.length-1 - idx]
}
).join('').replace(/^一十/, '十')
}

console.log(toChineseNumeral(29389))
console.log(toChineseNumeral(29000))
console.log(toChineseNumeral(20001))
console.log(toChineseNumeral(20901))

console.log([
10,
11,
18,
21,
110,
123,
24681
].map(toChineseNumeral))
``````