Add more test cases.
// 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))
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Add more test cases.